vb.net barcode generator open source Waiting for an Asynchronous Method to Complete in C#

Creator Data Matrix 2d barcode in C# Waiting for an Asynchronous Method to Complete

Waiting for an Asynchronous Method to Complete
Make Data Matrix ECC200 In C#
Using Barcode printer for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix ECC200 Reader In C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The result produced by calling a Begin method is an IAsyncResult. I have discarded these in the previous examples, but you can use them to wait for an asynchronous method to complete. This is similar to the Task.Wait method seen in 24. Listing 25-4 demonstrates waiting for asynchronous method. Listing 25-4. Waiting for an Asynchronous Method to Complete using System; class Listing 04 { public delegate long PerformCalc(int start, int end, int increment); static void Main(string[] args) { // assign the deleate PerformCalc myDelegate = PerformCalcMethod; // call the method several times
Draw PDF-417 2d Barcode In C#.NET
Using Barcode generator for .NET framework Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
Encoding GTIN - 128 In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create UCC - 12 image in .NET applications.
www.OnBarcode.com
CHAPTER 25 ASYNCHRONOUS METHODS AND PARALLEL LOOPS
Barcode Encoder In Visual C#
Using Barcode maker for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Print QR Code ISO/IEC18004 In C#
Using Barcode creator for .NET framework Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, AsyncMethodCallback, myDelegate); IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, AsyncMethodCallback, myDelegate); IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, AsyncMethodCallback, myDelegate); Console.WriteLine("Async methods are running..."); // wait for each of the async methods to complete res1.AsyncWaitHandle.WaitOne(); res2.AsyncWaitHandle.WaitOne(); res3.AsyncWaitHandle.WaitOne(); Console.WriteLine("Async methods have all completed"); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } static void AsyncMethodCallback(IAsyncResult asyncResult) { // get the delegate that is being called from the result long result = ((PerformCalc)asyncResult.AsyncState).EndInvoke(asyncResult); // write out the result Console.WriteLine("Result: {0}", result); } static long PerformCalcMethod(int start, int end, int increment) { long result = 0; for (int i = start; i < end; i += increment) { result += i; } return result; } } The IAsyncResult.WaitHandle method returns a System.Threading.WaitHandle object and if you call the WaitOne method, the main thread or current Task will block until the asynchronous method that the IAsyncResult relates to has completed. In Listing 25-4, the WaitOne method is called for each of the IAsyncResults that were created from the BeginInvoke method calls. Here is the result of compiling and running Listing 25-4: Async methods are running... Result: 36028796750528512 Result: 576460750692810753 Result: 2305843005992468481 Async methods have all completed Press enter to finish
Encoding Barcode In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Creating EAN8 In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create EAN8 image in .NET applications.
www.OnBarcode.com
CHAPTER 25 ASYNCHRONOUS METHODS AND PARALLEL LOOPS
Painting Data Matrix In Java
Using Barcode printer for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Data Matrix 2d Barcode Recognizer In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
You can make waiting for an asynchronous method more like waiting for a Task by using the IAsyncResult returned from BeginInvoke to call the EndInvoke method. This may seem a little odd, but when you do this, the EndInvoke method blocks until the asynchronous method has finished executing. When using this technique, you do not need to provide a callback method, as demonstrated in Listing 25-5. Listing 25-5. Using EndInvoke to Wait for an Asynchronous Method using System; class Listing 05 { public delegate long PerformCalc(int start, int end, int increment); static void Main(string[] args) { // assign the deleate PerformCalc myDelegate = PerformCalcMethod; // call the method several times IAsyncResult res1 = myDelegate.BeginInvoke(0, int.MaxValue, 1, null, myDelegate); IAsyncResult res2 = myDelegate.BeginInvoke(0, int.MaxValue / 2, 1, null, myDelegate); IAsyncResult res3 = myDelegate.BeginInvoke(0, int.MaxValue / 4, 4, null, myDelegate); Console.WriteLine("Async methods are running..."); foreach (IAsyncResult res in new IAsyncResult[] { res1, res2, res3 }) { long result = myDelegate.EndInvoke(res); Console.WriteLine("Result: {0}", result); } Console.WriteLine("Async methods have all completed"); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } static long PerformCalcMethod(int start, int end, int increment) { long result = 0; for (int i = start; i < end; i += increment) { result += i; } return result; } }
Make GS1-128 In None
Using Barcode creator for Software Control to generate, create GS1-128 image in Software applications.
www.OnBarcode.com
Recognize UPC-A Supplement 5 In Visual C#.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
CHAPTER 25 ASYNCHRONOUS METHODS AND PARALLEL LOOPS
Painting Barcode In Java
Using Barcode printer for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Encode DataMatrix In Java
Using Barcode encoder for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
Compiling and running Listing 25-5 produces the following output: Async methods are running... Result: 2305843005992468481 Result: 576460750692810753 Result: 36028796750528512 Async methods have all completed Press enter to finish
2D Barcode Creation In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Matrix Barcode image in .NET applications.
www.OnBarcode.com
Barcode Maker In Java
Using Barcode creation for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Mixing Asynchronous Methods and Tasks
Creating EAN-13 In Java
Using Barcode generator for Android Control to generate, create EAN13 image in Android applications.
www.OnBarcode.com
DataMatrix Generator In VS .NET
Using Barcode creator for .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
The TPL provides support for mixing Tasks and asynchronous methods. The advantage of doing this is to include existing code based on asynchronous methods (which pre-date Tasks in .NET) in a continuation chain of Tasks. Listing 25-6 provides a demonstration of creating a Task that uses asynchronous methods. Listing 25-6. Creating a Task for Asynchronous Methods using System; using System.Net; using System.Threading.Tasks; class Listing 06 { static void Main(string[] args) { // create a Task that will asychronously perform a DNS lookup Task<IPHostEntry> dnsTask = Task<IPHostEntry>.Factory.FromAsync( Dns.BeginGetHostEntry, Dns.EndGetHostEntry, "www.microsoft.com", null); // create a continuation that consumes the result from the DNS query Task continuationTask = dnsTask.ContinueWith(antecedent => { Console.WriteLine("Result from DNS Task is: {0}", antecedent.Result.HostName); }); // wait for the continuation to complete continuationTask.Wait(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } You use the Task<T>.Factory.FromAsync method to create a Task from an asynchronous method; the parameters are the Begin method, the End method, the parameters to pass to the Begin method, and a
GTIN - 12 Drawer In Java
Using Barcode drawer for Java Control to generate, create UPC Code image in Java applications.
www.OnBarcode.com
Decoding Code 128 Code Set B In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.