print barcode in crystal report vb.net PARALLEL PROGRAMMING in C#.NET

Printer ECC200 in C#.NET PARALLEL PROGRAMMING

CHAPTER 24 PARALLEL PROGRAMMING
Data Matrix Drawer In C#
Using Barcode maker for VS .NET Control to generate, create DataMatrix image in .NET framework applications.
www.OnBarcode.com
Scan ECC200 In C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
// enumerate the exceptions for this Task foreach (Exception innerException in currentTask.Exception.InnerExceptions) { Console.WriteLine("Exception type for Task {0}: {1}", i, innerException.GetType()); Console.WriteLine("Exception message for Task {0}: {1}", i, innerException.Message); } } else { Console.WriteLine("Task {0} ran to completion", i); Console.WriteLine("Result for Task {0}: {1}", i, currentTask.Result); } } } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } public static long CalculateSum(int taskID, int x, long y) { if (taskID % 2 == 0 && x == 1000) { throw new ArgumentOutOfRangeException( string.Format("Exception for taskID {0}", taskID)); } else { return x + y; } } } In Listing 24-17, the catch clause of the try statement is used to read the IsFaulted property of each Task that has been created. You know that there has been at least one exception, because otherwise the catch clause wouldn t be invoked. You can then interrogate the Task objects to find out which ones have had problems. As you saw in Table 24-3, the IsFaulted property returns true if a Task has encountered an exception. You can get details by using the Exception property, which returns an AggregateException that you can process just as you did previously. Compiling and running Listing 24-17 produces the following results: Task 0 encountered an exception Exception type for Task 0: System.ArgumentOutOfRangeException Exception message for Task 0: Specified argument was out of the range of valid values. Parameter name: Exception for taskID 0 Task 1 ran to completion Result for Task 1: 2305843005992468481 Task 2 encountered an exception Exception type for Task 2: System.ArgumentOutOfRangeException Exception message for Task 2: Specified argument was out of the range of valid values. Parameter name: Exception for taskID 2 Task 3 ran to completion
Print GS1 - 13 In C#
Using Barcode generation for Visual Studio .NET Control to generate, create GTIN - 13 image in .NET framework applications.
www.OnBarcode.com
Making Barcode In C#
Using Barcode drawer for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
QR-Code Creator In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create QR image in .NET framework applications.
www.OnBarcode.com
Matrix 2D Barcode Maker In Visual C#.NET
Using Barcode printer for .NET Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Result for Task 3: 2305843005992468481 Press enter to finish From these results, you can see that the first and third Tasks you created encountered ArgumentOutOfRange exceptions and the second and fourth Tasks were able to run to completion and return results.
Barcode Maker In C#
Using Barcode drawer for .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
EAN-8 Supplement 2 Add-On Drawer In Visual C#
Using Barcode encoder for .NET framework Control to generate, create EAN-8 Supplement 2 Add-On image in Visual Studio .NET applications.
www.OnBarcode.com
Using a Custom Exception Escalation Policy
Data Matrix Drawer In VS .NET
Using Barcode maker for .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Encode Data Matrix In Java
Using Barcode generator for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
If you don t handle an exception in the Task body or by calling a trigger member and handling the resulting AggregateException, the exception is escalated. By default, this escalation results in the unhandled exceptions being thrown when your Task objects are finalized by the garbage collector (see 18 for details of finalization). Since you can t predict when the garbage collector will finalize a given Task, this means that an unhandled exception can cause your program to terminate unexpectedly. You can change the way unhandled exceptions are dealt with by implementing a custom escalation policy. You do this by subscribing to the static TaskScheduler.UnobservedTaskException event. The TaskScheduler class is in the System.Threading namespace; the other members of this class are for advanced Task management and not within the scope of this book. Listing 24-18 demonstrates implementing a custom exception escalation policy. Listing 24-18. Implementing a Custom Task Exception Escalation Policy using System; using System.Threading.Tasks; class Listing 18 { static void Main(string[] args) { TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs eventArgs) => { // mark the exception as being handled eventArgs.SetObserved(); // get the aggregate exception AggregateException aggEx = (AggregateException)eventArgs.Exception; // enumerate the exceptions Console.WriteLine("--- Exceptions handled by custom policy ---"); foreach (Exception innerEx in aggEx.InnerExceptions) { Console.WriteLine("Inner exception: {0} - {1}", innerEx.GetType(), innerEx.Message); } }; // create the tasks for (int i = 0; i < 4; i++) {
Barcode Decoder In Visual C#.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Reader In VB.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
Draw Barcode In Objective-C
Using Barcode maker for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Print Barcode In Visual Studio .NET
Using Barcode printer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Task.Factory.StartNew((stateObject) => { int taskID = (int)stateObject; long total = 0; for (int j = 0; j < int.MaxValue; j++) { total = CalculateSum(taskID, j, total); } Console.WriteLine("Task {0} ran to completion", taskID); }, i); } // prompt the user to press enter to run the GC Console.WriteLine("Press enter to run GC"); Console.ReadLine(); // run the GC GC.Collect(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } public static long CalculateSum(int taskID, int x, long y) { if (taskID % 2 == 0 && x == 1000) { Console.WriteLine("Throwing an exception for Task {0}", taskID); throw new Exception(string.Format("Exception for taskID {0}", taskID)); } else { return x + y; } } } The custom policy in Listing 24-18 is shown in bold. You register with the UnobservedTaskException event; when exceptions are not handled using the other techniques, your policy will be used by invoking your event handler. You can learn more about events and event handling in 10. The UnobservedTaskExceptionEventArgs class (from the System.Threading.Tasks namespace) is used to pass details of the unhandled exceptions to your policy code. The Exception property returns an AggregateException containing the unhandled exceptions, although you will need to cast to the AggregateException type before you can access the inner exceptions. The SetObserved method tells the TPL that your custom policy has handled the exception and that no further action is required. The default escalation policy will be used if you don t call the SetObserved method in your custom policy, allowing you to decide to selectively handle exceptions. The custom policy shown in Listing 24-18 calls the SetObserved method and enumerates the aggregated exceptions. The rest of Listing 24-18 creates a set of Tasks, some of which run to completion and some of which encounter exceptions. When the user presses the Enter key, the garbage collector is explicitly called, which causes the Task objects to be finalized (this assumes that the user doesn t press the Enter key until all of the Tasks have finished). The finalization of the Task objects causes the unhandled exceptions to be escalated to the custom policy. Compiling and running Listing 24-18 produces the following results: Press enter to run GC
Generating ECC200 In Objective-C
Using Barcode drawer for iPad Control to generate, create ECC200 image in iPad applications.
www.OnBarcode.com
Draw Barcode In Java
Using Barcode generation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Paint Data Matrix 2d Barcode In None
Using Barcode creation for Software Control to generate, create ECC200 image in Software applications.
www.OnBarcode.com
Draw UPC-A Supplement 5 In None
Using Barcode generator for Software Control to generate, create UPC A image in Software applications.
www.OnBarcode.com
Encoding USS-128 In None
Using Barcode generation for Word Control to generate, create EAN / UCC - 14 image in Microsoft Word applications.
www.OnBarcode.com
Scanning Code 3 Of 9 In .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.