EDN Admin
Well-known member
Why is the following ASP.NET code using PageAsyncTask executes in total of 8 seconds regardless if I run it as is or if I comment 2 lines with PageAsyncTask and un-comment Thread.Sleep(5000) in Page_load?:
I understand that PageAsyncTask runs a task in parallel to other tasks and that should make overall process of page loading faster in this case. What am I missing?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class _Default : System.Web.UI.Page
{
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; string foo(<span style="color:Blue; string param1, <span style="color:Blue; string param2);
<span style="color:Blue; public IAsyncResult BeginLongRunningTransaction(AsyncCallback cb, <span style="color:Blue; object state)
{
<span style="color:Blue; var arr = (<span style="color:Blue; string[]) state;
<span style="color:Blue; string z1 = arr[0];
<span style="color:Blue; string z2 = arr[1];
foo method = <span style="color:Blue; this.LongRunningTransaction;
<span style="color:Blue; return method.BeginInvoke(z1, z2, cb, state);
}
<span style="color:Blue; protected <span style="color:Blue; void Page_Load(<span style="color:Blue; object sender, EventArgs e)
{
Response.Write(<span style="color:#A31515; "Page_Load: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
<span style="color:Blue; string [] arr = {<span style="color:#A31515; "Zorik1", <span style="color:#A31515; "Zorik2"};
PageAsyncTask asyncTask1 = <span style="color:Blue; new PageAsyncTask(BeginAsyncOperation, EndAsyncOperation, OperationTimeOut, arr, <span style="color:Blue; true);
Page.RegisterAsyncTask(asyncTask1);
Page.ExecuteRegisteredAsyncTasks();
<span style="color:Green; //Thread.Sleep(5000);
Thread.Sleep(1000);
Thread.Sleep(1000);
Thread.Sleep(1000);
}
<span style="color:Blue; private IAsyncResult BeginAsyncOperation(<span style="color:Blue; object sender, EventArgs e,
AsyncCallback cb, <span style="color:Blue; object state)
{
<span style="color:Blue; return <span style="color:Blue; this.BeginLongRunningTransaction(cb, state);
}
<span style="color:Blue; private <span style="color:Blue; string LongRunningTransaction(<span style="color:Blue; string param1, <span style="color:Blue; string param2)
{
Thread.Sleep(5000);
txtAsync.Text = <span style="color:#A31515; "Updated";
updPnl1.Update();
<span style="color:Blue; return <span style="color:#A31515; "this is return string";
}
<span style="color:Blue; private <span style="color:Blue; void EndAsyncOperation(IAsyncResult ar)
{
}
<span style="color:Blue; private <span style="color:Blue; void OperationTimeOut(IAsyncResult asyncResult)
{
<span style="color:Blue; string a = <span style="color:#A31515; "";
}
<span style="color:Blue; protected <span style="color:Blue; void Page_PreRender(<span style="color:Blue; object s, EventArgs e)
{
<span style="color:Blue; string a = <span style="color:#A31515; "";
Response.Write(<span style="color:#A31515; "Pre-Render Time: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
}
<span style="color:Blue; protected <span style="color:Blue; void Page_PreRenderComplete(<span style="color:Blue; object s, EventArgs e)
{
Response.Write(<span style="color:#A31515; "Page_PreRenderComplete: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
}
}
[/code]
View the full article
Code:
//PageAsyncTask asyncTask1 = new PageAsyncTask(BeginAsyncOperation, EndAsyncOperation,<br/>
OperationTimeOut, arr, true); //Page.RegisterAsyncTask(asyncTask1);
Code:
Code:
Thread.Sleep(5000);
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class _Default : System.Web.UI.Page
{
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; string foo(<span style="color:Blue; string param1, <span style="color:Blue; string param2);
<span style="color:Blue; public IAsyncResult BeginLongRunningTransaction(AsyncCallback cb, <span style="color:Blue; object state)
{
<span style="color:Blue; var arr = (<span style="color:Blue; string[]) state;
<span style="color:Blue; string z1 = arr[0];
<span style="color:Blue; string z2 = arr[1];
foo method = <span style="color:Blue; this.LongRunningTransaction;
<span style="color:Blue; return method.BeginInvoke(z1, z2, cb, state);
}
<span style="color:Blue; protected <span style="color:Blue; void Page_Load(<span style="color:Blue; object sender, EventArgs e)
{
Response.Write(<span style="color:#A31515; "Page_Load: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
<span style="color:Blue; string [] arr = {<span style="color:#A31515; "Zorik1", <span style="color:#A31515; "Zorik2"};
PageAsyncTask asyncTask1 = <span style="color:Blue; new PageAsyncTask(BeginAsyncOperation, EndAsyncOperation, OperationTimeOut, arr, <span style="color:Blue; true);
Page.RegisterAsyncTask(asyncTask1);
Page.ExecuteRegisteredAsyncTasks();
<span style="color:Green; //Thread.Sleep(5000);
Thread.Sleep(1000);
Thread.Sleep(1000);
Thread.Sleep(1000);
}
<span style="color:Blue; private IAsyncResult BeginAsyncOperation(<span style="color:Blue; object sender, EventArgs e,
AsyncCallback cb, <span style="color:Blue; object state)
{
<span style="color:Blue; return <span style="color:Blue; this.BeginLongRunningTransaction(cb, state);
}
<span style="color:Blue; private <span style="color:Blue; string LongRunningTransaction(<span style="color:Blue; string param1, <span style="color:Blue; string param2)
{
Thread.Sleep(5000);
txtAsync.Text = <span style="color:#A31515; "Updated";
updPnl1.Update();
<span style="color:Blue; return <span style="color:#A31515; "this is return string";
}
<span style="color:Blue; private <span style="color:Blue; void EndAsyncOperation(IAsyncResult ar)
{
}
<span style="color:Blue; private <span style="color:Blue; void OperationTimeOut(IAsyncResult asyncResult)
{
<span style="color:Blue; string a = <span style="color:#A31515; "";
}
<span style="color:Blue; protected <span style="color:Blue; void Page_PreRender(<span style="color:Blue; object s, EventArgs e)
{
<span style="color:Blue; string a = <span style="color:#A31515; "";
Response.Write(<span style="color:#A31515; "Pre-Render Time: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
}
<span style="color:Blue; protected <span style="color:Blue; void Page_PreRenderComplete(<span style="color:Blue; object s, EventArgs e)
{
Response.Write(<span style="color:#A31515; "Page_PreRenderComplete: " + DateTime.Now.ToString() + <span style="color:#A31515; "<br/>");
}
}
[/code]
View the full article