5,664,339 members and growing! (16,116 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Utilities     Intermediate License: The Code Project Open License (CPOL)

AsynchronousProcessing

By Emeka Awagu

An easier way to asynchronously process web page requests
C#, .NET, ASP.NET, Dev

Posted: 2 Apr 2008
Updated: 2 Apr 2008
Views: 6,030
Bookmarked: 17 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 2.75 Rating: 2.88 out of 5
1 vote, 12.5%
1
2 votes, 25.0%
2
2 votes, 25.0%
3
2 votes, 25.0%
4
1 vote, 12.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 using asynchronous processing everywhere on my websites. I wanted to centralise the way i performed these calls thoughout my application. I created a class which i called AsynchronousProcessingManager.

Using the code

The AsynchronousProcessingManager looks like this:

 
     public class AsynchronousProcessingManager
    {
        //fields
        AsynchronousTaskDelegate methodDelegate;
        public event AsyncProcessingEndedEventHandler OnAsyncProcessingEnded;

        //constructor
        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)
        {
            // AsynchronousProcessingManager apm =
            // (AsynchronousProcessingManager)result.AsyncState;
        }
    }    
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, this 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 the 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 retrieves performs a long running query in a database. I define the method as follows:
private AsyncResultObject DoQuery()
{
    AsyncResultObject aro = new AsyncResultObject();
    try
    {
        //query
        aro.ResultObject = //result of query
    }
    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)
    {
        //handle the exception
    }
    else
    {
        //do something with the aro.ResultObject
    }
} 
I also have a Timeout method
private void TimeOut(IAsyncResult ar)
{
    Response.Write("Try again later"); //or something along those lines
} 
So 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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Emeka Awagu



Occupation: Software Developer
Location: Nigeria Nigeria

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
QuestionAsynchronous processingmemberstixoffire14:01 8 Apr '08  
GeneralRe: Asynchronous processingmemberfodaley14:07 8 Apr '08  
GeneralNeeds a ProjectmemberDewey16:52 3 Apr '08  
GeneralRe: Needs a ProjectmemberEmeka Awagu2:29 4 Apr '08  
GeneralRe: Needs a Projectmemberosmofrilander21:59 8 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Apr 2008
Editor:
Copyright 2008 by Emeka Awagu
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project