Re: why does clicking a Button stop an unrelated async thread?

Tech-Archive recommends: Fix windows errors by optimizing your registry



If I understand you correctly, how do you know that the thread stopped?
Because it didn't communicate back to the page? When you click the other
button, the page context is over. A new context is in its place. Your
calling thread has no hook to return back to the previous page. Ideally, you
should get an exception in your worker thread because of an invalid context.
However, I suspect it might just go away or terminate the thread.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley


"James Irvine" <jamesNotReally@xxxxxxx> wrote in message
news:139a8v8mda07f28@xxxxxxxxxxxxxxxxxxxxx
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" />&nbsp;
<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>


.


Quantcast