Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Asynchronous Pages

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
18 Oct 2013CPOL2 min read 10.1K   5   1
Improve the scalability of a website and efficient design for time consuming processes.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Asynchronous Pages 

Objective: 

Improve the Scalability of website and efficient design for time consuming process.

Note: It should not be confused with Asynchronous request from Client side. AJAX used to improve the end user interface, where as Asynchronous pages handled in server side to provide efficient way of handling the request. It’s particularly used in web pages that include time consuming code that queries database.

Problem: 

.NET maintains a pool of threads to handle the incoming requests, when a request is received ASP.NET assigns a thread from its pool to process the request.

Let’s say we have a code that involves significant waiting time, so if all available threads were assigned to the incoming requests and its executing the time consuming code. The additional requests will be queued; if a limit is reached we will be getting “503 Server Unavailable errors”.

The possible waiting scenarios are: Large data from slow DBRead a file from Remote locationWeb service object…

Resolution: 

If we create Asynchronous pages with time consuming code executing in a separate thread pool which will free up the ASP.NET thread pool to allow it for processing other requests. Once a separate thread completes its work ASP.NET assigns an available thread to complete the operation. This is not going to improve the performance by any way, tricky way to handle all requests effectively making your application scalable.

Example:

Step 1:

HTML
<%@ Page Async="true" %> 

IHttpAsyncHandler will be implemented instead of IHttpHandler.

Step 2: 

C#
AddOnPreRenderCompleteAsync(New BeginEventHandler  
   _(Addressof BeginTask), New EndEventHandler (Addressof EndTask)) 

This will be called when the page loads. 

  • BeginEventHandler - Launches Asynchronous task.
  • EndEventHandler - Handles callback from Asynchronous task.
  • BeginTask and EndTask are the methods assigned to the above delegates.

The page lifecycle will be as follows:

  • Init
  • Load
  • Controlevents
  • Prerender
  • BeginTask()
  • Asynchonous task
  • End Task()
  • PreRenderComplete
  • Savestate
  • Render

Agenda:

  • Initial ASP.NET thread
  • Another ASP.NET thread
  • Thread outside of ASP.NET thread pool
This article was originally posted at http://wiki.asp.net/page.aspx/1483/asynchronous-pages

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
QuestionAny disadvantage/limitation of using Async pages Pin
PranavSingh23-Oct-13 3:36
professionalPranavSingh23-Oct-13 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.