Labels

Thursday, July 12, 2007

Asynchronous Events

We know about Asynchronous Delegates.

 

e.g

 

delegate string DownloadString (string uri);

DownloadString download1 = new DownloadString(Print);

IAsyncResult cookie1 = download1.BeginInvoke (uri1, null, null);

string s1 = download1.EndInvoke (cookie1);

 

 

public string Print(string url)

{

          Return “”;

}

 

 

Similar to Asynchronous Delegates, there are Asynchronous Events.

This is called the "event-based asynchronous pattern" and is distinguished by a method whose name ends with "Async", and a corresponding event whose name ends in "Completed".

 

So for above example – There would be DownloadStringAsync method. To use it, you first handle the "Completed" event (e.g. DownloadStringCompleted) and then call the "Async" method (e.g. DownloadStringAsync).

 

When the method finishes, it calls your event handler.

 

The event-based pattern also offers events for progress reporting and cancellation, designed to be friendly with Windows applications that update forms and controls. If you need these features in a type that doesn't support the event-based asynchronous model (or doesn't support it correctly!) you don't have to take on the burden of implementing the pattern yourself, however (and you wouldn't want to!) All of this can be achieved more simply with the BackgroundWorker helper class.

 

No comments:

Post a Comment