This is the second part in the series of Writing Asynchronous web pages in ASP.NET. In my last post, we discussed about various programming model for writing asynchronous code and wrote a asynchronous page using Asynchronous Programming model(APM). The link of part-1 of the series is given below
Writing Asynchronous Web Pages with ASP.NET- Part 1
In this post we’ll use another approach that is Event-based asynchronous pattern(EAP).
What is Event-based asynchronous Pattern (EAP)?
This model provides another way to write asynchronous code. In this pattern, it provides one void method (as a naming convention method name ends with async) and one or more events to notify the caller that there is a change in status. It means we don’t need to keep checking the status of the task, an event will be raised once the task will be completed. This asynchronous task runs in the background and simplest one, uses BackgroundWorker class to implement.
Any class that provides the capability to call its method asynchronously (using EAP model), provides a method that ends with async (naming convention) and an event that notifies when the task completes. It can provide more events to provide the status of the thread. We should follow these rules in our class if we want to write our own custom asynchronous methods that uses EAP Model.
So let’s create a class that uses this pattern
public class MyCustomClass { public void ReadCharCountAsync(byte[] buffee, int offset, int count); public event ReadCompletedEventHandler ReadCompleted; } public delegate void ReadCompletedEventHandler(object sender, ReadCompletedEventArgs readCompletedEventArgs); public class ReadCompletedEventArgs : AsyncCompletedEventArgs { public int Result { get; } }
We can see here that my class has one async method and one event handler. Then we defined the event handler and event arguments which contains the result of the method.
In this post, I’ll be using a .NET class WebClient that provides a method that can consumed asynchronously using EAP model. In this example, I am just downloading a page from the given URL and showing it on the screen. There could be many other scenarios.
I have created a webpage and put Async=”true” in the page directive in aspx file. This tells the page that this will be executed asynchronously. So let’s see the code behind
public partial class AsyncEAP : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { WebClient client = new WebClient(); Uri uri = new Uri("http://www.google.com/"); // Specify that the DownloadStringCallback method gets called // when the download completes. client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback); client.DownloadStringAsync(uri); } void DownloadStringCallback(object sender, DownloadStringCompletedEventArgs e) { if (e.Error == null) { renderedItem.InnerHtml = e.Result; } } }
So if we see the above code then we see that in Page_Load method after creating the instance of WebClient, I have subscribed the completed event and then called the DownloadStringAsync method. DownloadStringCallback method gets called once the download completes. Here I just checked if there is an error while executing and if there is no error then accessed the result. Here there is no way to find the status of the task but sure that once the method completes its processing, the completed event handler will be fired.
There are many other classes that provides the async feature using this model and we can use that. In my next post, We’ll use Task based asynchrony and see how can we leverage that in our ASP.NET pages.
Cheers,
Brij