Multithreading: blocking the thread by calling EndXXX directly

As we’ve seen, one of the available options is blocking the thread by calling the EndXXX method directly. This might be a good option when you only need to do one or two small tasks and then you need to wait until the asynchronous operation is completed.

In practical terms, this option won’t be usable in many scenarios. However, I’d say that this is the easiest of the four available options because you practically don’t need to make any changes to the existing synchronous algorithm you might be using (and ease of usage is important too, right?). To show how you might use this option, let’s try to get the HTML of a page through an asynchronous request. Here’s some code you might use for a synchronous approach:

var request = WebRequest.Create("",,);
//(1)do something else not related with request
WebResponse response;
try {
    response = request.GetResponse();
catch (WebException ex) {
//(2)or do something else not related with request
//then proceed and do work with info returned from request

As you can see, if we’ve got something that needs to be done and that doesn’t depend on the web request, we must put it before or after the GetResponse method invocation. During the method invocation, the thread is blocked until the current web request ends (which might happen due to a exception or when it gets the response from the web site).

This isn’t what we want in most cases. That’s where we can use the APM to help us do an asynchronous call and (probably) improve the performance of our algorithm. Here’s one of the possibilities for getting the response in an asynchronous fashion:

var request = WebRequest.Create("");
var result = request.BeginGetResponse(null, null);
//doing something else: could be (1) or/and (2) in previous sample
//when they complete, it’s time to block until we reiceve
//the response from the server
Console.WriteLine("async request fired at {0}", DateTime.Now);
WebResponse response;
try {
    response = request.EndGetResponse(result);
catch(WebException ex){
   //just catching webexceptions
   //(the method might throw other exceptions)

As you can see, we start by kicking the asynchronous task and then start executing other quick non blocking tasks which don’t depend on the data returned by the web request. When we’re done with those simple task, we call the EndGetResponse method.

If the asynchronous request has ended, the EndGetResponse method returns (in this case, the WebResponse instance which contains the response returned by the site) or throws (if there was an exception during the asynchronous request) immediately. If the asynchronous request hasn’t ended yet, then the thread will block until the asynchronous processing ends.

Notice that, in this case, we’re relying on the APM implementation for doing the right thing (ie, to return or throw immediately or to block until the asynchronous request ends).

On the next post we’ll keep looking at the other available options for waiting for the completion of an asynchronous operation started through the APM pattern. Keep tuned for more on multithreading.


~ by Luis Abreu on June 9, 2009.

Leave a Reply

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

You are commenting using your 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: