Newbie needs help with ThreadPool

R

Raymond Du

Hi,

This is my first mutiple threading progam, I have an AST.Net page
zThreadPool.aspx which has one button: button1 and one Label: label1. The
following code snippet does not work at all:

public partial class zThreadPool : System.Web.UI.Page
{

void CallbackProc(object obj1)
{
((Label)obj1).Text = DateTime.Now.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CallbackProc), Label1);
}

}

When I click on button1, label1 is not updated, no exceptions thrown. I put
a break point at

((Label)obj1).Text = DateTime.Now.ToString();

The debugger did break on this line.

Any Ideas?

TIA
 
J

Jon Skeet [C# MVP]

Raymond Du said:
This is my first mutiple threading progam, I have an AST.Net page
zThreadPool.aspx which has one button: button1 and one Label: label1. The
following code snippet does not work at all:

<snip>

How are you expecting threading to work here? By the time your callback
is called, the page will probably have been returned to the user. If
you need to wait for multiple things to happen before the page is
rendered, you'll need to write the synchronization logic for that
explicitly.

As a general rule, threading is less useful in ASP.NET apps than in
WinForms apps. ASP.NET apps are already multi-threaded as multiple
requests can be handled at the same time.

Multi-threading *can* be handy in web apps if you have a very long-
running operation to perform (e.g. a complex search) but then you need
to return a page to the user which will then poll until the operation
is complete.
 
G

Guest

Raymond,
You cannot do asynchronous processing in an "ASP.NET" page like this,
because the Page class instance is completely gone after the page has been
rendered. So, there is nothing to "callback" to.

You can do asynchronous page processing of methods in parallel, however.
Here is a short article that may help:

http://www.eggheadcafe.com/articles/20060918.asp
Peter
 

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

Top