P
Paul
Hi,
I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so the page can carry on rendering whilst the call is taking place and also returning the ASP.NET worker thread to service another page request (for scalability). I have developed a very basic asynchronous object (for testing, that reproduces the same error as my long running query object). Code for the object is as follows:
==========================================
using System;
using System.Threading;
public class TestAsyncClass
{
public TestAsyncClass(){}
private delegate string InvokeTest();
private InvokeTest deleg;
public string Test()
{
Thread.Sleep(1000);
return "Hello, World!";
}
public IAsyncResult BeginTest()
{
deleg = new InvokeTest(Test);
return deleg.BeginInvoke(null, null);
}
public string EndTest( IAsyncResult asyncResult)
{
return deleg.EndInvoke(asyncResult);
}
}
==========================================
The above class can be called from a very basic ASP.NET page as follows:
==========================================
TestAsyncClass testAsyncClass = new TestAsyncClass();
protected void Page_Load(object sender, EventArgs e)
{
IAsyncResult iasyncResult = testAsyncClass.BeginTest();
while (!iasyncResult.IsCompleted)
{
Response.Write("<Br>...Waiting");
Thread.Sleep(500);
}
Response.Write("<Br>" + testAsyncClass.EndTest(iasyncResult));
}
==========================================
As I'd expect, it produces the following:
...Waiting
...Waiting
Hello, World!
However, I don't think the above is really doing what I want, as it ties up the page waiting for it to finish. If I attempt to call the async class using the code below, which I believe will free up the page to do other stuff, then the request always times out (which is my problem).
==========================================
TestAsyncClass testAsyncClass = new TestAsyncClass();
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(BeginTest, EndTest, TestTimedOut, null, true);
Page.AsyncTimeout = new TimeSpan(0, 0, 5);
Page.RegisterAsyncTask(task);
}
public IAsyncResult BeginTest(object sender, EventArgs e, AsyncCallback callBack, object extraData)
{
Response.Write("<Br><Br>Calling Begin Test");
return testAsyncClass.BeginTest();
}
public void EndTest(IAsyncResult result)
{
Response.Write("<Br>...Successful: ");
Response.Write( testAsyncClass.EndTest( result));
}
public void TestTimedOut(IAsyncResult result)
{
Response.Write("<Br>...Timed Out");
Response.End();
}
==========================================
Produces the following:
Calling Begin Test
...Timed Out
I don't think I'm passing the IAsyncResult around properly or I'm confusing the two Asynchronous calls. I'm pretty sure that the issue is in my object "TestAsyncClass()" as my the above web page code using "PageAsyncTask" is pretty much ripping off any example call to "SqlCommand.BeginExecuteReader()" and "SqlCommand.EndExecuteReader()". Can anyone throw any light on the problem / tell me the error of my ways. Note: the only change I made to the web page, other than "Page_Load()" is to add Async="true" to the @Page declaration.
Thanks in advance,
- Paul.
I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so the page can carry on rendering whilst the call is taking place and also returning the ASP.NET worker thread to service another page request (for scalability). I have developed a very basic asynchronous object (for testing, that reproduces the same error as my long running query object). Code for the object is as follows:
==========================================
using System;
using System.Threading;
public class TestAsyncClass
{
public TestAsyncClass(){}
private delegate string InvokeTest();
private InvokeTest deleg;
public string Test()
{
Thread.Sleep(1000);
return "Hello, World!";
}
public IAsyncResult BeginTest()
{
deleg = new InvokeTest(Test);
return deleg.BeginInvoke(null, null);
}
public string EndTest( IAsyncResult asyncResult)
{
return deleg.EndInvoke(asyncResult);
}
}
==========================================
The above class can be called from a very basic ASP.NET page as follows:
==========================================
TestAsyncClass testAsyncClass = new TestAsyncClass();
protected void Page_Load(object sender, EventArgs e)
{
IAsyncResult iasyncResult = testAsyncClass.BeginTest();
while (!iasyncResult.IsCompleted)
{
Response.Write("<Br>...Waiting");
Thread.Sleep(500);
}
Response.Write("<Br>" + testAsyncClass.EndTest(iasyncResult));
}
==========================================
As I'd expect, it produces the following:
...Waiting
...Waiting
Hello, World!
However, I don't think the above is really doing what I want, as it ties up the page waiting for it to finish. If I attempt to call the async class using the code below, which I believe will free up the page to do other stuff, then the request always times out (which is my problem).
==========================================
TestAsyncClass testAsyncClass = new TestAsyncClass();
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(BeginTest, EndTest, TestTimedOut, null, true);
Page.AsyncTimeout = new TimeSpan(0, 0, 5);
Page.RegisterAsyncTask(task);
}
public IAsyncResult BeginTest(object sender, EventArgs e, AsyncCallback callBack, object extraData)
{
Response.Write("<Br><Br>Calling Begin Test");
return testAsyncClass.BeginTest();
}
public void EndTest(IAsyncResult result)
{
Response.Write("<Br>...Successful: ");
Response.Write( testAsyncClass.EndTest( result));
}
public void TestTimedOut(IAsyncResult result)
{
Response.Write("<Br>...Timed Out");
Response.End();
}
==========================================
Produces the following:
Calling Begin Test
...Timed Out
I don't think I'm passing the IAsyncResult around properly or I'm confusing the two Asynchronous calls. I'm pretty sure that the issue is in my object "TestAsyncClass()" as my the above web page code using "PageAsyncTask" is pretty much ripping off any example call to "SqlCommand.BeginExecuteReader()" and "SqlCommand.EndExecuteReader()". Can anyone throw any light on the problem / tell me the error of my ways. Note: the only change I made to the web page, other than "Page_Load()" is to add Async="true" to the @Page declaration.
Thanks in advance,
- Paul.