Asynchronous Programming—BeginInvoke EndInvoke

Jump to: navigation, search
Visual C# Tutorials

Introduction to Asynchronous Programming

© 2008 Daniel Solis

BeginInvoke and EndInvoke

Before we look at examples of the asynchronous programming patterns, let’s take a closer look at the BeginInvoke and EndInvoke methods. Some of the important things to know about BeginInvoke are the following:

  • When calling BeginInvoke, the actual parameters in the parameter list consist of the following:
  • The parameters required by the referenced method
  • Two additional parameters, called the callback parameter and the state parameter
  • BeginInvoke retrieves a thread from the thread pool and starts the referenced method running on the new thread.
  • BeginInvoke returns to the calling thread a reference to an object implementing the IAsyncResult interface. This interface reference contains information about the current state of the asynchronous method. The initial thread then continues execution.

The following code shows an example of calling a delegate's BeginInvoke method. The first line declares a delegate type called MyDel. The next line declares a method called Sum, which matches the delegate.

  • The following line declares a delegate object called del, of the delegate type MyDel, and initializes its invocation list with the Sum method.
  • Finally, the last line of code calls the BeginInvoke method of the delegate object, and supplies it with the two delegate parameters 3 and 5, and the two BeginInvoke parameters callback and state, which are set to null in this example. When executed, the BeginInvoke method performs two actions:
  • It gets a thread from the thread pool and starts method Sum running on the new thread, supplying it with 3 and 5 as its actual parameters.
  • It collects information about the state of the new thread and makes it available through a reference to an interface of type IAsyncResult, which it returns to the calling thread. The calling thread stores it in a variable called iar.
delegate long MyDel( int first, int second ); // Delegate declaration
static long Sum(int x, int y){ ... } // Method matching delegate
MyDel del = new MyDel(Sum); // Create delegate object
IAsyncResult iar =          // Information about new thread
   del.BeginInvoke          // Invoke delegate asynchronously 
   ( 3, 5,                  // Delegate params
     null, null );          // Extra params 

The EndInvoke method is used to retrieve the values returned by the asynchronous method call, and to release resources used by the thread. EndInvoke has the following characteristics:

  • It takes as a parameter the reference to the IAsyncResult returned by the BeginInvoke method, and finds the thread it refers to.
  • If the thread pool thread has exited, EndInvoke does the following:
  • It cleans up the exited thread's loose ends and disposes of its resources.
  • It finds the value returned by the referenced method and returns that value as its return value.
  • If the thread pool thread is still running when EndInvoke is called, the calling thread stops and waits for it to finish before cleaning up and returning the value. Because EndInvoke cleans up after the spawned thread, you must make sure that an EndInvoke is called for each BeginInvoke.
  • If the asynchronous method triggers an exception, the exception is raised when EndInvoke is called.

The following line of code shows an example of calling EndInvoke to retrieve the value from an asynchronous method. You must always include the reference to the IAsyncResult object as a parameter.

long result =    // Return value from async method 
   del.EndInvoke // Delegate object
      ( iar );   // IAsyncResult object 

EndInvoke supplies all the output from the asynchronous method call, including ref and out parameters. If a delegate's referenced method has ref or out parameters, they must be included in EndInvoke's parameter list before the reference to the IAsyncResult object, as shown here:

long result =                 // Return value from async method
      (out someInt,           // out param
       iar);                  // IAsyncResult object 

prevpp.png  nextpp.png
C# Online.NET