Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Asynchronous page processing in ASP.net webforms (.NET 2.0)

Can someone provide me with a simple example of Asynchronous page processing in ASP.NET Webforms 2.0 (I'm using VS 2010, so new syntax like lambdas are ok)?

I have some long running requests that I don't want tying up IIS threads.

For simplicity's sake, let's say my current code looks like this:

protected void Page_Load(object sender, EventArgs e)
{
    string param1 = _txtParam1.Text;
    string param2 = _txtParam2.Text;

    //This takes a long time (relative to a web request)
    List<MyEntity> entities = _myRepository.GetEntities(param1, param2);

    //Conceptually, I would like IIS to bring up a new thread here so that I can
    //display the data after it has come back.
    DoStuffWithEntities(entities);

}

How can I modify this code so that it is asynchronous? Let's assume that I already set async="true" in the aspx page.

EDIT

I think I figured out how to get what I'm looking for. I've put the example code in an answer here. Feel free to point out any flaws or changes that can be made.

like image 512
Giovanni Galbo Avatar asked Nov 16 '10 03:11

Giovanni Galbo


People also ask

What is asynchronous in asp net?

An asynchronous request takes the same amount of time to process as a synchronous request. For example, if a request makes a web service call that requires two seconds to complete, the request takes two seconds whether it is performed synchronously or asynchronously.

What is Aspnet UseTaskFriendlySynchronizationContext?

aspnet:UseTaskFriendlySynchronizationContext. Specifies how asynchronous code paths in ASP.NET 4.5 behave.

Is ASP net synchronous?

The requests in the system depend on some external resource like user request. When a request comes in, ASP.NET takes one of its thread pool threads and assigns it to that request. The request handler will call that external resource synchronously.

What is async controller in MVC why one should use it?

The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. It does not mean it will take lesser time to complete the action.


2 Answers

Here is a simple example of asynchronous processing.

   protected void Page_Load(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        ThreadPool.QueueUserWorkItem(state => Dokimes_Programming_multithread_QueryWorkThead.ThreadProc2());

        Debug.Write("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        // Thread.Sleep(4000);

        txtDebug.Text += "ended";

        Debug.Write("end.");
    }


    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {

        // No state object was passed to QueueUserWorkItem, so  stateInfo is null.
        Debug.Write(" Hello from the thread pool 1.");
    }

    static void ThreadProc2()
    {
        // No state object was passed to QueueUserWorkItem, so  stateInfo is null.
        Debug.Write("Hello from the thread pool 2.");
    }

Other way

You can use the PageAsyncTask, see here a full example:
http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask.aspx

Something like

clAsynCustomObject oAsynRun = new clAsynCustomObject();

PageAsyncTask asyncTask = new PageAsyncTask(oAsynRun.OnBegin, oAsynRun.OnEnd, oAsynRun.OnTimeout, null, true);
Page.RegisterAsyncTask(asyncTask);
Page.ExecuteRegisteredAsyncTasks();
like image 90
Aristos Avatar answered Oct 07 '22 13:10

Aristos


I asked some folks on the ASP.NET team. Here's their emailed response to me, and now, to you.

All that code ends up doing is spinning up a new thread and performing delegate invocation on that thread. So now there are two threads running: the request thread and the new thread. Hence this sample actually has worse performance than the original synchronous code would have had.

See http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 for a sample on how to write and consume async methods in ASP.NET.

like image 44
Scott Hanselman Avatar answered Oct 07 '22 12:10

Scott Hanselman