Asynchronous Programming in .NET – Part 2

Nov 5th, 2010 | By | Category: Framework & Libraries

In the last post we have discussed about the Asynchronous Programming Model(APM) and it’s different implementations.In this post we will discuss about the Event Based Asynchronous Pattern in .NET Framework which was introduced in .NET 2.0.This pattern makes use of the event/delegate model of the .NET Framework and is particularly useful for GUI based components.

For a synchronous method XXX the asynchronous version will have:

  • A method named XXXAsync with same parameters or an additional instance of System.Object to represent the state of the asynchronous call.This method initiates the asynchronous execution in a different thread.
  • A class for event arguments as shown below.This class inherits from System.ComponentModel.AsyncCompletedEventArgs
public class XXXCompletedEventArgs : AsyncCompletedEventArgs

         The AsyncCompletedEventArgs event args class exposes the following properties:

    • Cancelled – Indicates whether the operation is canceled or not.
    • Error – stores the Exception object that might have been generated during execution of the asynchronous method
    • UserState – stores the state of that asynch call in a System.Object instance.
  • A delegate XXXCompletedEventHandler as shown below:
public delegate void XXXCompletedEventHandler(object sender,XXXCompletedEventArgs e)
  • An event of type XXXCompletedEventHandler.This event is raised when the asynchronous operation completes

The System.ComponentModel.BackgroundWorker class provides an implementation of this Event Based Asynchronous Pattern.It provides a method RunWorkerAsync which triggers the asynchronous execution.It also exposes two events:

  • DoWork – This event is fired when RunWorkerAsync is called.
  • RunWorkerCompleted – This event is fired when the asynchronous operation has completed.

The following method is expected to something asynchronously:

private void DoSomething(object sender, DoWorkEventArgs e)
{
    Thread.Sleep(100);
    MessageBox.Show("Test....");
}

There is another method which needs to be fired when the asynchronous operation completes:

private void DoComplete(object sender, RunWorkerCompletedEventArgs e)
{
    MessageBox.Show("Completed..." + e.Result.ToString());
}

This two methods are hooked to the BackgroundWorker component as shown below:

this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.DoSomething);
            this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.DoComplete);

The detailed implementation sample for this pattern is available at:

http://msdn.microsoft.com/en-us/library/9hk12d4y.aspx


Kick It on DotNetKicks.com
Tags: ,
  • http://topsy.com/trackback?url=http%3A%2F%2Fcodingndesign.com%2Fblog%2F%3Fp%3D192&utm_source=pingback&utm_campaign=L2 Tweets that mention Asynchronous Programming in .NET – Part 2 – Coding N Design — Topsy.com

    [...] This post was mentioned on Twitter by Abhijit Jana, Sankarsan Bose. Sankarsan Bose said: Asynchronous Programming in .NET – Part 2 http://dlvr.it/84HXY [...]

  • http://tryfly.com/?p=1184 Posts about Programming from google blogs as of November 5, 2010 « tryfly.com

    [...] [...]