Implemented as two methods
- BeginOperation
- Begines operation asynchronously in a background thread from thread pool
- Returns an object of type IAsyncResult
- EndOperation
- Get results from the operation
Accessing results of an async operation
- Need to wait for results from async operation
- Call EndOperation from applications’s main method to block main application execution
// Start the asynchronous request for DNS information. // This example does not use a delegate or user-supplied object // so the last two arguments are null. IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); Console.WriteLine("Processing your request for information..."); // Do any additional work that can be done here. try { // EndGetHostByName blocks until the process completes. IPHostEntry host = Dns.EndGetHostEntry(result);
- Use AsyncWaitHandle to block main application execution
// Start the asynchronous request for DNS information. IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); Console.WriteLine("Processing request for information..."); // Wait until the operation completes. result.AsyncWaitHandle.WaitOne(); // The operation completed. Process the results. try { // Get the results. IPHostEntry host = Dns.EndGetHostEntry(result);
- Call EndOperation from applications’s main method to block main application execution
- Check IsComplete property periodically and call EndOperation when completed
// Start the asychronous request for DNS information. IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); Console.WriteLine("Processing request for information..."); // Poll for completion information. // Print periods (".") until the operation completes. while (result.IsCompleted != true) { UpdateUserInterface(); } // The operation is complete. Process the results. // Print a new line. Console.WriteLine(); try { IPHostEntry host = Dns.EndGetHostEntry(result);
- Use an AsyncCallback delegate to specify a method to be invoked when async operation is completed
public static void Main() { // Create the delegate that will process the results of the // asynchronous request. AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation); string host; do { Console.Write( " Enter host computer or <enter></enter> to finish: "); host = Console.ReadLine(); if (host.Length > 0) { // Increment the request counter in a thread safe manner. Interlocked.Increment(ref requestCounter); // Start the asynchronous request for DNS information. Dns.BeginGetHostEntry(host, callBack, host); } } while (host.Length > 0); } // The following method is called //when each asynchronous operation completes. static void ProcessDnsInformation(IAsyncResult result) { string hostName = (string) result.AsyncState; hostNames.Add(hostName); try { // Get the results. IPHostEntry host = Dns.EndGetHostEntry(result); hostData.Add(host); } // Store the exception message. catch (SocketException e) { hostData.Add(e.Message); } finally { // Decrement the request counter in a thread-safe manner. Interlocked.Decrement(ref requestCounter); } }