Monday, July 6, 2009

Asynchronous Pages in ASP.NET 2.0

Scenario: When ASP.NET receives a page request, it is basically a thread from the thread pool and then considered as a request thread. To understand it simply, consider that the life cycle is Synchronous. Any intermediate Async process is considered as breaking the line of process since Async must run on a separate thread from there onwards. Situations some time tend us to use some process and we will be immediately thrown with the error below.

Error: “Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event”

Support: ASP.NET 1.1 version does not support it OOB but it has a back door entry to do it.

However in ASP.NET 2.0, its a very simple attribute setting on the page directive.

<% Page Async=”true”…...

Consider that the situation further demands that you need to set it on run time and not on design time on page.

So, one has to subscribe to the below event and do the rest of the magic.

AddOnPreRenderCompleteAsync (
new BeginEventHandler(MyBeginMethod),
new EndEventHandler (MyEndMethod)

for more detailed information, view this blog

No comments: