J
James Irvine
I launch an asynchronous thread by clicking ButtonInitAsyncThread, which
works fine. When it's done, it sends back it's results. But, while
this thread is still running, if I click on ButtonCheckThreadStatus,
which has nothing to do with the async thread running, it cancels the
thread, and the thread never sends back the results. What causes this?
thanks -James
The complete C# code-behind:
using System;
using System.Threading;
public partial class testThreads : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Async delegate used to call a method with this signature
asynchronously
public delegate long SampSyncSqrDelegate(long i);
protected void ButtonInitAsyncThread_Click(object sender, EventArgs
e) // async call
{
long inParm = Convert.ToInt32(TextBoxInParm.Text);
long callResult = -1;
WorkerClass sampSyncObj = new WorkerClass();
// launch longWindedMethod method Asynchronously:
SampSyncSqrDelegate sampleDelegate = new
SampSyncSqrDelegate(sampSyncObj.longWindedMethod);
IAsyncResult aResult = sampleDelegate.BeginInvoke(inParm, null,
null);
//Wait for the call to complete
aResult.AsyncWaitHandle.WaitOne();
// get the output returned back:
callResult = sampleDelegate.EndInvoke(aResult);
LabelThreadStatus.Text = Convert.ToString(callResult);
}
protected void ButtonCheckThreadStatus_Click(object sender,
EventArgs e)
{
Label1.Text = Convert.ToString(DateTime.Now);
}
}
public class WorkerClass
{
// A method that does some time-consuming work - returns the number
passed in + 'a':
public long longWindedMethod(long longIn)
{
int a = 0;
while (a < 5)
{
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine(a);
a++;
}
return a + longIn;
}
}
And the complete aspx listing:
<%@ Page Language="C#" MasterPageFile="~/MasterPageGold.master"
AutoEventWireup="true" CodeFile="why.aspx.cs" Inherits="testThreads"
Title="Untitled Page" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolderForBody" Runat="Server">
<br />
<asp:Button ID="ButtonInitAsyncThread" runat="server" Text="init
async thread" OnClick="ButtonInitAsyncThread_Click" />
<asp:TextBox ID="TextBoxInParm" runat="server">78</asp:TextBox>
<asp:Label ID="LabelThreadStatus" runat="server" Text="thread
status"></asp:Label><br />
<br />
<br />
<asp:Button ID="ButtonCheckThreadStatus" runat="server"
Text="CheckThreadStatus" OnClick="ButtonCheckThreadStatus_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>
works fine. When it's done, it sends back it's results. But, while
this thread is still running, if I click on ButtonCheckThreadStatus,
which has nothing to do with the async thread running, it cancels the
thread, and the thread never sends back the results. What causes this?
thanks -James
The complete C# code-behind:
using System;
using System.Threading;
public partial class testThreads : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Async delegate used to call a method with this signature
asynchronously
public delegate long SampSyncSqrDelegate(long i);
protected void ButtonInitAsyncThread_Click(object sender, EventArgs
e) // async call
{
long inParm = Convert.ToInt32(TextBoxInParm.Text);
long callResult = -1;
WorkerClass sampSyncObj = new WorkerClass();
// launch longWindedMethod method Asynchronously:
SampSyncSqrDelegate sampleDelegate = new
SampSyncSqrDelegate(sampSyncObj.longWindedMethod);
IAsyncResult aResult = sampleDelegate.BeginInvoke(inParm, null,
null);
//Wait for the call to complete
aResult.AsyncWaitHandle.WaitOne();
// get the output returned back:
callResult = sampleDelegate.EndInvoke(aResult);
LabelThreadStatus.Text = Convert.ToString(callResult);
}
protected void ButtonCheckThreadStatus_Click(object sender,
EventArgs e)
{
Label1.Text = Convert.ToString(DateTime.Now);
}
}
public class WorkerClass
{
// A method that does some time-consuming work - returns the number
passed in + 'a':
public long longWindedMethod(long longIn)
{
int a = 0;
while (a < 5)
{
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine(a);
a++;
}
return a + longIn;
}
}
And the complete aspx listing:
<%@ Page Language="C#" MasterPageFile="~/MasterPageGold.master"
AutoEventWireup="true" CodeFile="why.aspx.cs" Inherits="testThreads"
Title="Untitled Page" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolderForBody" Runat="Server">
<br />
<asp:Button ID="ButtonInitAsyncThread" runat="server" Text="init
async thread" OnClick="ButtonInitAsyncThread_Click" />
<asp:TextBox ID="TextBoxInParm" runat="server">78</asp:TextBox>
<asp:Label ID="LabelThreadStatus" runat="server" Text="thread
status"></asp:Label><br />
<br />
<br />
<asp:Button ID="ButtonCheckThreadStatus" runat="server"
Text="CheckThreadStatus" OnClick="ButtonCheckThreadStatus_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>