Multithreading: APM and options for waiting until work completes

In the last post, we’ve started looking at oldest asynchronous pattern of the .NET framework: the APM model. Today, we’re going to list the available options for waiting to the completion of an asynchronous operation started through a framework that implements APM pattern.

After kicking off an asynchronous operation, we have four available options:

  • we can block the thread until work is completed by calling the EndXXX method directly. This is a good approach when we have a couple of extra things to do and, after that, we really need to wait until the asynchronous operation completes;
  • we can wait on the IAsyncResult’s WaitHandle (AsyncWaitHandle property) until the work that is being executed in parallel ends;
  • we can poll the IAsyncResult’s IsCompleted property. This is also a good option when we want to perform other tasks while the asynchronous operation is running;
  • we can pass a callback (WaitCallback parameter passed to the BeginXXX method) that is to be called when the operation completes. This is a good option when we don’t need to block the “main” thread until the operation completes and relies on a “continuation passing” style, where a closure is executed when the async operation ends.

As you can see, the APM model gives us several options  and we’ll take a detailed look at each in the next posts. Keep tuned for more.

Advertisements

~ by Luis Abreu on June 8, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: