vb.net barcode generator open source Providing a Continuation Result in C#

Encoder Data Matrix in C# Providing a Continuation Result

Providing a Continuation Result
Printing Data Matrix 2d Barcode In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
Recognizing Data Matrix ECC200 In Visual C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
If you want your continuation Task to produce a result, you can use the ContinueWith<T> method, where T is the result type. The antecedent Task can produce a different type of result or no result at all. Listing 24-21 shows a continuation producing a different type of result to its antecedent.
Print UPC A In Visual C#
Using Barcode maker for VS .NET Control to generate, create UCC - 12 image in .NET framework applications.
www.OnBarcode.com
Create PDF417 In C#.NET
Using Barcode generation for .NET framework Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
Barcode Encoder In Visual C#.NET
Using Barcode maker for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Draw GS1 - 13 In C#
Using Barcode encoder for .NET Control to generate, create GS1 - 13 image in VS .NET applications.
www.OnBarcode.com
Listing 24-21. Returning a Result from a Continuation using System; using System.Threading.Tasks; class Listing 21 { static void Main(string[] args) { Task<long> firstTask = new Task<long>(() => { Console.WriteLine("First task starting"); long result = 0; for (int i = 0; i < int.MaxValue; i++) { result += i; } Console.WriteLine("First task complete"); return result; }); Task<int> secondTask = firstTask.ContinueWith<int>((Task<long> antecedent) => { Console.WriteLine("Second task starting"); // get the result and status from the antecedent task Console.WriteLine("Result from antecedent: {0}", antecedent.Result); Console.WriteLine("Status from antecedent: {0}", antecedent.Status); // perform the work long result = 0; for (int i = 0; i > int.MinValue; i--) { result += i; } Console.WriteLine("Second task complete"); return (int)(result + antecedent.Result); }); // start the first task firstTask.Start(); // wait for both tasks to complete Task.WaitAll(firstTask, secondTask); // print out the result from the continuation Task Console.WriteLine("Continuation result: {0}", secondTask.Result); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } The antecedent in Listing 24-21 is a Task<long> and the continuation is a Task<int>, created by calling ContinueWith<int>. The continuation takes the long result from the antecedent and combines it
Printing UCC-128 In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create GTIN - 128 image in .NET framework applications.
www.OnBarcode.com
Encoding Postnet In C#.NET
Using Barcode printer for .NET framework Control to generate, create USPS POSTal Numeric Encoding Technique Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
Printing Data Matrix 2d Barcode In VS .NET
Using Barcode creator for Reporting Service Control to generate, create DataMatrix image in Reporting Service applications.
www.OnBarcode.com
Generate Data Matrix ECC200 In Java
Using Barcode creation for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
with the output of its own work to create an int result. Compiling and running Listing 24-21 produces the following output: First task starting First task complete Second task starting Result from antecedent: 2305843005992468481 Status from antecedent: RanToCompletion Second task complete Continuation result: -2147483647 Press enter to finish
USS Code 128 Generator In None
Using Barcode drawer for Word Control to generate, create ANSI/AIM Code 128 image in Word applications.
www.OnBarcode.com
ANSI/AIM Code 39 Printer In None
Using Barcode generator for Font Control to generate, create Code-39 image in Font applications.
www.OnBarcode.com
Creating Chains of Continuations
Barcode Recognizer In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Making USS-128 In None
Using Barcode generator for Online Control to generate, create UCC.EAN - 128 image in Online applications.
www.OnBarcode.com
You can chain together more than two Tasks. In fact, you can chain together as many as you want simply by calling the ContinueWith method. Listing 24-22 shows you how to do this. Listing 24-22. Creating a Longer Chain of Tasks using System; using System.Threading.Tasks; class Listing 22 { static void Main(string[] args) { // create the first task Task<int> firstTask = new Task<int>(() => { Console.WriteLine("First Task Started"); // do some simple work and return the result return 10 + 20; }); // create the second task Task<int> secondTask = firstTask.ContinueWith<int>((Task<int> antecedent) => { Console.WriteLine("Second Task Started"); // do some simple work and combine with the antecdent result return 30 + antecedent.Result; }); // create the third task Task<int> thirdTask = secondTask.ContinueWith<int>((Task<int> antecedent) => { Console.WriteLine("Third Task Started"); // do some simple work and combine with the antecedent result return 40 + antecedent.Result; }); // create the fourth task Task finalTask = thirdTask.ContinueWith((Task<int> antecedent) => {
Code128 Creation In None
Using Barcode creator for Online Control to generate, create Code128 image in Online applications.
www.OnBarcode.com
Decoding QR-Code In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
Generate Barcode In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
ANSI/AIM Code 128 Scanner In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Console.WriteLine("Final Task Started"); // do some simple work and combine with the antecedent result int finalResult = antecedent.Result * 10; Console.WriteLine("Final Task Finished"); }); // start the first Task firstTask.Start(); // wait for the final Task to complete finalTask.Wait(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Listing 24-22 creates a chain of four Tasks. Most of them are Task<int> objects, but the last one is a plain Task (I did this just for variety; there are no restrictions on what kinds of Task you can chain together). Once you have created the continuation chain, start the first Task; each Task is started as its antecedent finishes. The result of compiling and running Listing 24-22 is as follows: First Task Started Second Task Started Third Task Started Final Task Started Final Task Finished Press enter to finish
Drawing UPC A In None
Using Barcode encoder for Online Control to generate, create UPC-A Supplement 5 image in Online applications.
www.OnBarcode.com
Decode Data Matrix In Visual C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Creating Selective Continuations
The ContinueWith method creates a continuation that will be performed regardless of what happens to the antecedent Task, even if the antecedent is canceled or encounters an exception. Listing 24-23 provides a demonstration. Listing 24-23. Using a Selective Continuation using System; using System.Threading; using System.Threading.Tasks; class Listing 23 { static void Main(string[] args) { // create a token source for cancellation CancellationTokenSource tokenSource = new CancellationTokenSource();
Copyright © OnBarcode.com . All rights reserved.