Asynchronous operations that use IAsyncResult objects

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);
       
  • No need to wait for async operation
    • 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 &gt; 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 &gt; 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); 
          } 
      }
  • This entry was posted in c#. Bookmark the permalink.

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.