Last post Feb 11, 2009 11:19 AM by betamax
Feb 07, 2009 09:44 AM|betamax|LINK
I have been trying to learn async programming model in ASP.NET and was following the article at
http://msdn.microsoft.com/en-us/magazine/cc163463.aspx (Figure 2). The article states to set Page directive async attribute to true and add async=true; to connection string. I did both of
these, copied the code and ran it. However, the task does not run asynchronously. There is a delay in page rendering and the page only outputs to browser when query has completed. I added a WAITFOR DELAY sql to the SELECT QUERY to simulate a network delay.
Here is the code:
public partial class AsyncDataBind : System.Web.UI.Page
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataReader _reader;
protected void Page_Load(object sender, EventArgs e)
IAsyncResult BeginAsyncOperation (object sender, EventArgs e,
AsyncCallback cb, object state)
string connect = WebConfigurationManager.ConnectionStrings
_connection = new SqlConnection(connect);
_command = new SqlCommand(
"SELECT title_id, title, price FROM titles", _connection);
return _command.BeginExecuteReader (cb, state);
void EndAsyncOperation(IAsyncResult ar)
_reader = _command.EndExecuteReader(ar);
protected void Page_PreRenderComplete(object sender, EventArgs e)
Output.DataSource = _reader;
public override void Dispose()
if (_connection != null) _connection.Close();
The code obviously works since it is taken from MSDN Magazine. But, from what I understand about async, the page should render without delay and the query should continue executing on another thread in the background. when the query finishes execution, results
can be displayed. So, the part where async operations are suppoed to return immediately IS NOT WORKING. I have tried numerous examples on async programming model but none have worked so far in my case.
Are there other settings that I need to set? Does this work on Visual Studio itself or do i need to run through IIS?
OR, is it supposed to be like this and I have the whole concept of async programming incorrect????
Please do let me know as I have spent weeks on learning this and whenever I run an example I seem to be doing it synchronously.
Feb 11, 2009 01:38 AM|Nai-Dong Jin - MSFT|LINK
I'm not sure if the result you want is something like Ajax controls, if so, that's called page's partitial refreshing technology. But here, what you are talking is the Asynchronous pages in ASP.NET 2.0.
Generally, in asynchronous pages, it udergoes its normal processing lifecycle until shortly after the PreRender event fires. At that time, ASP.NET calls the Begin method that you registered using AddOnPreRenderCompleteAsync, and some I/O bound operations
such as database query lunches. And at this moment, the thread assigned to the reqeust goes back to the thread pool. After the asynchronous operation has complete, at that time ASP.NET extracts a thread from the thread pool to call the End method you registered.
So as you can see, between the time begin returns and end gets called, the reqeust-process thread is free to serve other reqeusts, and until End is called, rendering is delayed.
Feb 11, 2009 11:19 AM|betamax|LINK
thanks for your reply.
I have read on numerous places on the Internet where the case to use async model is that the control is returned immediately to the client.
What I get from the above statement is that an async operation runs in the background and the user can continue working on the web site. When the background operation completes, the UI is automatically updated.
There are numerous web sites where I have read this. Am I getting something wrong?