posts - 217, comments - 218, trackbacks - 239

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Asynchronous Calls in asp.net2.0

.Net Framework 2.0 makes life so easy with asynchronous calls from pages. I remember in asp.net1.x we had to do so much to achieve the same behaviour, but we are lucky that in asp.net2.0 its very easy.

Again there are more than one ways to implement asynchronous calls in asp.net2.0 and you may be asking which pattern to use.

The first way is to use the AddOnPreRenderCompleteAsync

AddOnPreRenderCompleteAsync (
    new BeginEventHandler(BeginAsyncMethod),
    new EndEventHandler (EndAsyncMethod)
);
The second way is to use the PageAsyncTask and RegisterAsyncTask
PageAsyncTask task = new PageAsyncTask( 
new BeginEventHandler(BeginAsyncMethod),
new EndEventHandler(EndAsyncMethod),
new EndEventHandler(TimeoutAsyncMethod), null
); 
RegisterAsyncTask(task);
 
The second way is preferrably better as it has the following advantages over the first way.
  • RegisterAsyncTask also take an additional parameter for timeout.
  • More than one RegisterAsyncTask can be called in one Request.
  • The fourth parameter of RegisterAsyncTask can take state of the Begin Method.
  • To the End the Timeout Method RegisterAsynTask gives you handy objects (ie HttpContext.Current, impersonation and culture).

Remember to put Async="true" attribute in the @ Page directive for any of the preferred methods mentioned above.

Also for Webservices we can use this following pattern:

theproxy.MyMethodCompleted += new MyMethodCompletedEventHandler (OnMyMethodCompleted);
theproxy.MyMethodAsync (...);
...
void OnMyMethodCompleted (Object source, MyMethodCompletedEventArgs e)
{
    // This is called when MyMethod completes
}
For any long running tasks always try to use AsyncMethods as it never ties up threads from the thread pool.
In msdn you will find this great article in this topic: Asynchronous Pages in asp.net 2.0. 

Print | posted on Monday, November 20, 2006 1:38 AM |

Feedback

Gravatar

# re: Asynchronous Calls in asp.net2.0

khjas;lkjhdfKJEF
4/2/2007 10:56 AM | sumit

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 4 and type the answer here:

Powered by: