Introduction
The idea of asynchronous processing in ASP.NET has always been very appealing. However, I found it a bit tedious to use and wanted a more manageable way to use asynchronous processing everywhere on my website. I wanted to centralize the way I performed these calls throughout my application. So, I created a class which I called AsynchronousProcessingManager
.
Using the code
The AsynchronousProcessingManager
looks like this:
public class AsynchronousProcessingManager
{
AsynchronousTaskDelegate methodDelegate;
public event AsyncProcessingEndedEventHandler OnAsyncProcessingEnded;
public AsynchronousProcessingManager(AsynchronousTaskDelegate atd)
{
this.methodDelegate = atd;
}
public IAsyncResult BeginAsyncTask(object sender,
EventArgs e, AsyncCallback ac, object state)
{
IAsyncResult result =
this.methodDelegate.BeginInvoke(ac, state);
return result;
}
public void EndAsyncTask(IAsyncResult result)
{
AsynchronousTaskDelegate cd =
(AsynchronousTaskDelegate)((AsyncResult)result).AsyncDelegate;
AsyncResultObject aro = cd.EndInvoke(result);
if (OnAsyncProcessingEnded != null)
{
AsyncEndedEventArgs ae = new AsyncEndedEventArgs(aro);
OnAsyncProcessingEnded(this, ae);
}
this.methodDelegate = null;
cd = null;
}
public void AsyncCallBack(IAsyncResult result)
{
}
}
The Begin and End AsyncTasks are necessary for the ASP.NET PageAsyncTask
class. In the EndAsyncTask
, I fire an event of type AsyncProcessingEndedEventHandler
which contains an object of type AsyncResultObject
. The AsynchronousProcessingManager
also has a field, 'methodDelegate
', of type AsynchronousTaskDelegate
, which will hold a reference to the method to be called. The 'methodDelegate
' returns an AsyncResultObject
which is contained in the AsyncEndedEventArgs
:
public delegate AsyncResultObject AsynchronousTaskDelegate();
public delegate void AsyncProcessingEndedEventHandler(object sender,
AsyncEndedEventArgs ae);
public class AsyncEndedEventArgs
{
AsyncResultObject resultObject;
public AsyncEndedEventArgs(AsyncResultObject rObject)
{
this.resultObject = rObject;
}
public AsyncResultObject ResultObject
{
get { return this.resultObject; }
set { this.resultObject = value; }
}
}
Within the body of the method, I instantiate an AsynchronousProcessingManager
with a constructor that takes the AsynchronousTaskDelegate
. I then wire the OnAsyncProcessingEnded
event to a method supplied as one of the parameters. This is the method that will be called after the the async processing has finished. I then call ASP.NET's RegisterAsyncTask
for the current page that is passed as a parameter as well.
As an example, I have a page called default.aspx. I want to call a method that performs a long running query in a database. I define the method as follows:
private AsyncResultObject DoQuery()
{
AsyncResultObject aro = new AsyncResultObject();
try
{
aro.ResultObject =
}
catch(Exception e)
{
aro.Exception = e;
}
return aro;
}
I also define the method to be called at the end of the async processing:
private void DoQueryEnded(object sender, AsyncEndedEventArgs ae)
{
AsyncResultObject aro = ae.ResultObject;
if(aro.Exception != null)
{
}
else
{
}
}
I also have a Timeout
method:
private void TimeOut(IAsyncResult ar)
{
Response.Write("Try again later");
}
To call the method from the page, I simply write:
ProcessAsyncTask(new AsynchronousTaskDelegate(DoQuery),
new AsyncProcessingEndedEventHandler(DoQueryEnded),
new EndedEventHandler(TimeOut), this);
I use this model throughout my application and it seems to work fine...so far!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.