Wow! I just took a break from work and watched the space shuttle launch. Amazing. Beautiful. Awesome. Words can't do it justice. My wife and kids went to TechEd with me this year, and while we were there, we toured the Kennedy Space Center and got a close-up look at the two space shuttle launch pads (which happen to be the same pads used for the Apollo moon missions). It made this morning's launch more exciting.
If you watched the launch on Fox News, you saw a shuttle astronaut named Tom Jones providing commentary. Tom has flown on four shuttle missions. My son and I met him and had our picture taken with him last month at Kennedy Space Center. Tom gave us a first-hand account of what the first couple of minutes after launch is like. (Lots of shaking and rattling.)
On a slightly different subject, this weekend I put the finishing touches on the final edits to October's Wicked Code column. The subject: asynchronous pages in ASP.NET 2.0. There's very little documentation on the subject in beta 2 (or in the trade magazines), but async pages are ultra-cool and are something EVERY ASP.NET developer should know about. Here's the code-behind class for an async page that uses async ADO.NET to do asynchronous data binding:
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)
{
if (!IsPostBack)
{
// Hook PreRenderComplete event for data binding
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
// Register async methods
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
}
}
IAsyncResult BeginAsyncOperation (object sender, EventArgs e, AsyncCallback cb, object state)
{
string connect = WebConfigurationManager.ConnectionStrings["PubsConnectionString"].ConnectionString;
_connection = new SqlConnection(connect);
_connection.Open();
_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;
Output.DataBind();
}
public override void Dispose()
{
if (_connection != null)
_connection.Close();
base.Dispose();
}
}
That's just one way to leverage the new async features of Whidbey. Lots more in October's Wicked Code.
On Jul 26 2005 11:22 AMBy jprosise
any commnet on the following:- Jeff Prosise 's article on MSDNMAG on async pages Now about the small gotcha:-If you are following the same article and runnign the code you ll found that :- The Databind.aspx is not returnign the result.The problem was however due to the fact that the Page_PreRenderComplete method is getting called twice and this behavior is there for all pages including the webservice calls. I ve the RC build and I am now goign to send mail to verify this behavior.if you ve faced similar problem here is the workaround:- to restrict the Page_PreRenderComplete get called twice you can use void EndAsyncOperation(IAsyncResult ar) { _reader = _command.EndExecuteReader(ar); this.PreRenderComplete -= new EventHandler(Page_PreRenderComplete); } else make check on empty datareader protected void Page_PreRenderComplete(object sender, EventArgs e) { if (_reader.Read()) { Gridview1.DataSource = _reader; Gridview1.DataBind(); } } but still the page_prerendercomplete ll invoked twice .
Thanks for a wonderful article. I have a scenario, where i have two datagrids on the page. They are independent and are bound to results from two different stored procedures. I want to bind them asynchronously so that both procs get executed at the same time. Since both procs take same amount of time, I am guessing that I can reduce the time by 50%. Do you ahve any code snippet which can help me in doing this or any pointers are appreciated. Thanks