vb.net barcode generator open source PARALLEL PROGRAMMING in C#

Encoder Data Matrix ECC200 in C# PARALLEL PROGRAMMING

CHAPTER 24 PARALLEL PROGRAMMING
Print Data Matrix 2d Barcode In Visual C#
Using Barcode generator for VS .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
Data Matrix Decoder In Visual C#.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
// create a Task Task<long> firstTask = new Task<long>(() => { Console.WriteLine("First task started"); long result = 0; for (int i = 0; i < int.MaxValue; i++) { result += i; if (tokenSource.Token.IsCancellationRequested) { Console.WriteLine("Task cancelled"); tokenSource.Token.ThrowIfCancellationRequested(); } } return result; }, tokenSource.Token); firstTask.ContinueWith((Task<long> antecedent) => { Console.WriteLine("Ran to completion continuation - antecedent result is: {0}", antecedent.Result); }, TaskContinuationOptions.OnlyOnRanToCompletion); firstTask.ContinueWith((Task<long> antecedent) => { Console.WriteLine("Canceled continuation"); }, TaskContinuationOptions.OnlyOnCanceled); // start the first task firstTask.Start(); // prompt the user to cancel the token Console.WriteLine("Press enter to cancel"); Console.ReadLine(); // cancel the token tokenSource.Cancel(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Selective continuations are created by providing a value from the TaskContinuationOptions enumeration as a parameter to the Task.ContinueWith method. In Listing 24-23, there are two selective continuations. The first uses the OnlyRanToCompletion enum value; this means that the continuation will only run if the antecedent completes without being cancelled or encountering an unhandled exception. The second continuation uses the OnlyOnCanceled value; this means that the continuation Task will run only if the antecedent has been canceled. The values from the TaskContinuationOptions enum are described in Table 24-4.
EAN-13 Supplement 5 Creation In Visual C#
Using Barcode generation for .NET Control to generate, create GS1 - 13 image in .NET framework applications.
www.OnBarcode.com
Print Barcode In Visual C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
PDF-417 2d Barcode Creator In C#
Using Barcode creator for .NET Control to generate, create PDF 417 image in Visual Studio .NET applications.
www.OnBarcode.com
2D Barcode Encoder In Visual C#
Using Barcode maker for Visual Studio .NET Control to generate, create Matrix Barcode image in .NET framework applications.
www.OnBarcode.com
Table 24-4. TaskContinuationOptions Values
Linear Creator In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create Linear 1D Barcode image in VS .NET applications.
www.OnBarcode.com
Make Code11 In C#
Using Barcode generator for VS .NET Control to generate, create Code 11 image in .NET framework applications.
www.OnBarcode.com
Value
Creating Data Matrix ECC200 In None
Using Barcode drawer for Word Control to generate, create DataMatrix image in Office Word applications.
www.OnBarcode.com
DataMatrix Decoder In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
NotOnRanToCompletion
Read Code 128 Code Set B In VB.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Painting UPC - 13 In None
Using Barcode encoder for Software Control to generate, create EAN 13 image in Software applications.
www.OnBarcode.com
Description
Code-39 Generator In None
Using Barcode creation for Online Control to generate, create Code 39 image in Online applications.
www.OnBarcode.com
Make PDF417 In Java
Using Barcode generation for Android Control to generate, create PDF417 image in Android applications.
www.OnBarcode.com
The continuation will run if the antecedent has been canceled or has encountered an unhandled exception. The continuation will run if the antecedent has not encountered an unhandled exception. The continuation will run if the antecedent has not been canceled. The continuation will run if the antecedent has not been canceled or has not encountered an unhandled exception. The continuation will run if the antecedent has encountered an unhandled exception The continuation will run if the antecedent has been canceled
Drawing PDF-417 2d Barcode In VB.NET
Using Barcode maker for Visual Studio .NET Control to generate, create PDF417 image in .NET framework applications.
www.OnBarcode.com
UPC-A Supplement 2 Encoder In Java
Using Barcode drawer for Android Control to generate, create GS1 - 12 image in Android applications.
www.OnBarcode.com
NotOnFaulted
Printing EAN13 In Objective-C
Using Barcode printer for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
Code 128C Generation In None
Using Barcode printer for Font Control to generate, create Code 128B image in Font applications.
www.OnBarcode.com
NotOnCanceled OnlyOnRanToCompletion
Barcode Creator In .NET
Using Barcode printer for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
QR Generator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create QR image in ASP.NET applications.
www.OnBarcode.com
OnlyOnFaulted
OnlyOnCanceled
Using one of these values when you use the ContinueWith method allows you to limit the conditions under which a continuation will be run or, as in the case of Listing 24-23, create different continuations to be run under different circumstances. If the user presses the Enter key while Listing 24-23 is running, the antecedent Task will be cancelled and the following output will be displayed: Press enter to cancel First task started Task cancelled Press enter to finish Canceled continuation If the user doesn t press the Enter key, the antecedent will run to competition and the other continuation Task will be run instead. This produces different results: Press enter to cancel First task started Ran to completion continuation - antecedent result is: 23058429812964066
Sharing Data between Tasks
The Tasks in the examples so far in this chapter have worked in isolation. This is ideal for demonstrating the features of the TPL, but not at all realistic. Most of the time, you will need to share data between
CHAPTER 24 PARALLEL PROGRAMMING
Tasks, and this can be troublesome unless you take the time to coordinate the way that the Tasks operate. Listing 24-24 shows the kind of problem that can arise when Tasks share data in an uncoordinated way. Listing 24-24. Problems Arising when Sharing Data Between Tasks using System; using System.Threading.Tasks; class Listing 24 { static void Main(string[] args) { // define a shared counter int counter = 0; // create an array of Tasks Task[] taskArray = new Task[10]; // create and start a series of Tasks that will share the counter for (int i = 0; i < taskArray.Length; i++) { taskArray[i] = Task.Factory.StartNew(() => { // enter a loop and increment the counter for (int j = 0; j < 1000; j++) { // increment the shared counter counter++; } }); } // wait for all of the Tasks to complete Task.WaitAll(taskArray); // print out the value of the shared counter Console.WriteLine("Counter value: {0}", counter); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } In Listing 24-24, ten Tasks are created. Each enters a for loop that increments a shared int variable 1,000 times. The main thread waits for all of the Tasks to finish and then prints out the value of the shared variable. If you have ten Tasks and each of them increments the int 1,000 times, then you would expect the value of the counter to be 10,000. And that s the result you get well, sometimes. When I compiled and ran Listing 24-24 on my PC, I got a result of 10,000 about 50% of the time. The result of the time, I got a different result, like this one: Counter value: 9132
Copyright © OnBarcode.com . All rights reserved.