Home > .NET Framework > Waiting for something to happen – Manual and AutoResetEvents

Waiting for something to happen – Manual and AutoResetEvents

Assume we have a scenario like this. A component has to call 3 web services to get the data. Instead of calling them one by one in a serial fashion, the component calls them asynchronously. After the component has all the three sets of data, let us say that the component processes the data. It needs to know when all the three method calls complete. One way of doing this is by polling for completion of the three async calls.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        while(service1Done == false && service2Done  == false && service3Done == false) {
            Thread.Sleep(500);
        }
 
        // Do the processing
 
    }
    private bool service1Done = false;
    private bool service2Done = false;
    private bool service3Done = false;
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done == true;
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done == true;
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done == true;
    }
}
 
But polling is generally not a good solution this problem. This can be solved using ManualResetEvents. The main thread calls WaitAll with the three ManualResetEvent objects (one each for a web service call). The same code using ManualResetEvent is given below.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        WaitHandle[] handles = new WaitHandle[] { service1Done, service2Done , service3Done }
        WaitHandle.WaitAll(handles);
         
        // Do the processing
 
    }
    private ManualResetEvent service1Done = new MaunalResetEvent(false);
    private ManualResetEvent service2Done =  = new MaunalResetEvent(false);
    private ManualResetEvent service3Done =  = new MaunalResetEvent(false);
 
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done.Set();
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done.Set();
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done.Set();
    }
}
 
When a ManualResetEvent is created it is created in non-signalled state. Calling the static method WaitAll on the WaitHandle class passing the three manual reset events in non-signalled state puts the calling thread into WaitSleepJoin state. Once all the three service calls are done (and all the set methods are called) all the manual reset events are signalled now. This makes the main thread schedulable again.
 
Assume the samething happens in a loop using a auto reset event eliminates the need for calling reset.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Do the statements below in a loop
     // begin loop
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        WaitHandle[] handles = new WaitHandle[] { service1Done, service2Done , service3Done }
        WaitHandle.WaitAll(handles);
         
        // Do the processing
      // end loop
 
    }
    private AutoResetEvent service1Done = new AutoResetEvent(false);
    private AutoResetEvent service2Done =  = new AutoResetEvent(false);
    private AutoResetEvent service3Done =  = new AutoResetEvent(false);
 
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done.Set();
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done.Set();
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done.Set();
    }
}

 

Advertisements
Categories: .NET Framework
  1. Unknown
    October 26, 2006 at 5:39 am

    Reptiles & Threading have some thing  common !!!! By n large guys hate both of them.I have not written any multithreaded program so far & may be a bad multithreaded app might be like having a one (reptile) in the wrong place.:)
     
    Having said that i would like to say that this is one of those article’s which would actually give the reader some comfort n  confidence to think abt multithreaded programming

  1. November 19, 2010 at 10:59 pm

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: