Kicking off asynchronous processes from an ASP.NET page

Sometimes you need to kick off long running tasks from within an ASP.NET page. There’s plenty of examples out there showing you how to get the results of a Web Request (useful if you are calling a web service, for example) or if you are making a database call through one of the provided methods in ADO.NET, but it was not very easy to find a good source of information on what is needed when you want to provide that functionality for your own long-running methods. So the setup is: You have an ASPX page calling down to your Business layer and your Business method takes a while to do what you asked it to do before it comes back. So it would be nice if you are able to spend that time allowing your page to do everything else that it needs to do. Once the page is done with all other processing, the thread gets put on hold and allows other resources to be processed by the server and then the thread gets signaled when the long-running task completes and that causes the page to finish processing and return control back to the user. NOTE that the user can’t move on and do other stuff in the meantime, there’s other patterns for that (ie. polling), which is not what this is for.

Before:

Business layer method:

public string LongRunningTask(string inputValue)
{
  Thread.Sleep(30000);
  return "result";
}

aspx code snippet (Action initiated by a button on the screen:

protected void btnRequest_Click(object sender, EventArgs e)
{
  .
  .
  .
  string _Result = new Business.Utility().LongRunningTask("blah");     .
  .
  .
}

So in this case, the return would come back after 30 seconds, and any additional functionality would wait for that statement to complete. This is what we need to do in order to make this an asynchronous call:

After:

Business layer:
– Create a delegate for your long-running method:

public delegate string LongRunningTaskDelegate(string inputValue);

– You add “Begin” and “End” methods, using the IAsyncResult interface:

public IAsyncResult BeginLongRunningTask(string inputValue, AsyncCallback callback, object asyncState)
{
   return new LongRunningTaskDelegate(LongRunningTask).BeginInvoke(inputValue, callback, asyncState);
}
public string EndLongRunningTask(IAsyncResult ar)
{
  return ((LongRunningTaskDelegate)((AsyncResult)ar).AsyncDelegate).EndInvoke(ar);
}

aspx code:

protected void btnRequest_Click(object sender, EventArgs e)
{
  .
  .
  .
  Page.RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(BeginLongRunningTask),
                                               new EndEventHandler(EndLongRunningTask),
                                               null,
                                               true));
  .
  .
  .
}

The four parameters for PageAsyncTask are the Begin Handler, End Handler, Timeout Handler, and your object state. You can get pretty creative on how you use this in your own environment, and it will help you get a little bit more throughput out of your pages.

About esteban

Esteban is the Founder and Chief Technologist at Nebbia Technology, an ALM consulting and Azure-powered technology company. He is a software developer with a passion for ALM, TFS, Azure, and software development best practices. Esteban is a Microsoft Visual Studio ALM MVP and ALM Ranger, Pluralsight author, and the president of ONETUG (Orlando .NET User Group).

One thought on “Error when installing SQL 2008 on Windows Server 2008 R2 (64 bit)

  1. Mark Allen

    Another approach which works well is to have the XML tag reference a custom TFS group. In my case there is a master contributor group shared between projects so rather than list all the contributor users in the template AssignedTo list boxes I created a TFS group called ‘TeamAssignments’ and within security I referenced the team. Then in the templates I added the tag ListItem value=[project]\TeamAssignments. Now when anyone is added to the team they are displayed in the AssignedTo List box. This also means that another team can be created without changing the templates as the changes can be done in security.

    TFS Team Name: Blue (example set up in security)
    TFS Group Name: TeamAssignments
    Members: Team Blue


Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *