why does clicking a Button stop an unrelated async thread?

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" />&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>
 
B

bruce barker

it doesn't. the button request runs against a different class instance
(and probably a different thread).

web application are stateless. the browser requests a page, asp.net
creates a page class instance to build the html, sends the html back,
then disposes the class. when a button is hit, the process starts over.

in your case you click the button which starts a long running request.
if you click another button, the browser close the connection and starts
a new request. this is a new class instance, so it knows nothing of the
last request.

by the way the server will complete the first request, but find when it
tries to send it back to the browser, the connection is closed.

-- bruce (sqlwork.com)
 
J

James Irvine

bruce said:
it doesn't. the button request runs against a different class instance
(and probably a different thread).

web application are stateless. the browser requests a page, asp.net
creates a page class instance to build the html, sends the html back,
then disposes the class. when a button is hit, the process starts over.

in your case you click the button which starts a long running request.
if you click another button, the browser close the connection and starts
a new request. this is a new class instance, so it knows nothing of the
last request.

by the way the server will complete the first request, but find when it
tries to send it back to the browser, the connection is closed.

-- bruce (sqlwork.com)

Thank you, sir. I did get it working as a Forms app, but couldn't do the same in asp. Now I know why.
 
A

Alvin Bruney [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top