ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – using regular web handler

As an ASP.NET developer we frequently deal with long running tasks for example:

  • Making long-lasting webservice calls.
  • Making prolonged calls to database that runs complex queries.
  • Do lengthy file operation
  • Calling remote objects etc.

When the server is busy performing the lengthy time consuming task/tasks, the poor user has to wait for the response. The most common technique used to interact with user is to display a loader icon or show some kind of progress bar on the page until the task is complete. However we can provide user immediate feedback of the task progress/state while the task is running in the Server and make the page more interactive/informative.

 

 iframefeedback

Here we will discuss several techniques to give end user instant feedback on the browser about task progress in  ASP.NET pages.

such as display:

 

Download


Display task progress using regular web handler:

This is very easy to code, all we need to do is write a regular web handler that runs the long running task and we have to keep flushing the response when state changes, in other words we decide to flush when the task reaches certain stage. The data is pushed to the the outgoing stream and the browser immediately displays the output. The connection to the browser is kept open until the task is complete.

RegularHandler

We have created a regular web handler that inherits from IHttpHandler and simulated a long running task by forcing the thread to sleep. However before the thread goes to sleep it Flushes the response using Response.Flush() method. The Flush method sends buffered output immediately. Normally all of the response is sent to the browser only when page completes its processing. This may be a issue for a long running task as the end user will not see anything until the page is complete. The Flush method comes handy in this situation and can send buffered output periodically to the browser. Imagine you will have to display 500 records and you can flush the buffer every 20 records until it is complete. The good thing of doing this is – user can see some data immediately, visualize the progress of the report and the server memory is also released on every Flush operation.

One more thing to note is the Response.IsClientConnected property which indicates the server whether the browser is still connected or has already left.  A very common scenario is user starts the long expensive task but decided to close the browser before the task is complete. When a browser requests the page from the server but does not wait for the entire page to be downloaded, the server continues to process the request, wasting CPU cycles. The Response.IsClientConnected property can determine if the browser is still connected or not. So when we perform a long task it is always a good idea to periodically test  whether the browser is still connected or not.

UseHandler 
A web handler file works just like a aspx page and can be directly browsed via a browser. The web handler above, returns response type text/html and we have pointed an anchor tag to the web handler. Please note a web handler is recommended for a performance critical web page.

I have started with a regular synchronous web handler, however it is recommended to use asynchronous web handlers that implements IHttpAsyncHandler for long running tasks. ASP.NET has limited number of threads in the thread pool and a normal synchronous page holds on to the thread for the duration of the request, and this thread is prevented to be used to process any other request during that period. As a result the thread gets stuck until the lengthy request is complete. If all the threads are stuck doing lengthy tasks the subsequent additional requests starts to queue up and when the queue fill up ASP.NET starts spitting 503 “Server Unavailable” errors.

ASP.NET provides a neater solution to this problem via Asynchrounous web handlers that implements the IHtttpAsyncHandler. The interface suggests to implement two additional methods BeginProcessRequest method and  EndProcessRequest. The idea is to spawn  an additional thread by hand in response to BeginProcessRequest, then perform the lengthy task in this additional thread and notify when the task is complete so the worker thread can return a response to the user.  We will look at Asynchronous web handlers in a later post in more details. Lets continue to look at other techniques now.

Published Tue, Oct 27 2009 3:08 by shahed

Comments

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – by streaming regular ASP.NET page

Monday, October 26, 2009 11:20 AM by Shahed Khan (MVP C#)

Here in this series of post I am discussing few techniques to give client instant feedback on task progress

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – in Asynchronous ASP.NET pages

Monday, October 26, 2009 11:52 AM by Shahed Khan (MVP C#)

  In the earlier post we have discussed little bit about the limited number of worker threads available

# ASP.NET MVC, ASP.NET Tips: Provide feedback to the user on Long Running Tasks – using loader image - MS AJAX and JQuery

Monday, October 26, 2009 12:20 PM by Shahed Khan (MVP C#)

It is quite common to display a loader-image or some kind of progress bar image in a browser when the

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – using XMLHttpRequest call

Monday, October 26, 2009 12:41 PM by Shahed Khan (MVP C#)

XMLHttpRequest (XHR) is a DOM API that can be used inside a web browser scripting language, such as JavaScript

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – using IFrame and web handlers

Monday, October 26, 2009 1:19 PM by Shahed Khan (MVP C#)

The <iframe> tag defines an inline frame that contains another document and is supported in all

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – by streaming regular ASP.NET page

Tuesday, October 27, 2009 12:13 PM by Shahed

Here in this series of post I am discussing few techniques to give client instant feedback on task progress

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – in Asynchronous ASP.NET pages

Tuesday, October 27, 2009 12:17 PM by Shahed

  to download the source code please click here In the earlier post we have discussed little bit

# ASP.NET MVC, ASP.NET Tips: Provide feedback to the user on Long Running Tasks – using loader image - MS AJAX and JQuery

Tuesday, October 27, 2009 12:22 PM by Shahed

It is quite common to display a loader-image or some kind of progress bar image in a browser when the

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – using XMLHttpRequest call

Tuesday, October 27, 2009 12:24 PM by Shahed

XMLHttpRequest (XHR) is a DOM API that can be used inside a web browser scripting language, such as JavaScript

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – by streaming regular ASP.NET page

Sunday, November 15, 2009 8:26 AM by Shahed Khan (MVP C#)

Here in this series of post I am discussing few techniques to give client instant feedback on task progress...

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – in Asynchronous ASP.NET pages

Sunday, November 15, 2009 8:31 AM by Shahed Khan (MVP C#)

to download the source code please click here In the earlier post we have discussed little bit...

# ASP.NET MVC, ASP.NET Tips: Provide feedback to the user on Long Running Tasks – using loader image - MS AJAX and JQuery

Sunday, November 15, 2009 8:34 AM by Shahed Khan (MVP C#)

It is quite common to display a loader-image or some kind of progress bar image in a browser when the...

# ASP.NET MVC, ASP.NET Tips: Provide immediate feedback to the browser on Long Running Tasks – using XMLHttpRequest call

Sunday, November 15, 2009 8:36 AM by Shahed Khan (MVP C#)

XMLHttpRequest (XHR) is a DOM API that can be used inside a web browser scripting language, such as JavaScript,...

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Powered by Community Server (Commercial Edition), by Telligent Systems