Groups | Blog | Home
all groups > c# > april 2007 >

c# : Newbie needs help with ThreadPool



Raymond Du
4/29/2007 9:01:32 PM
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



Jon Skeet [C# MVP]
4/30/2007 12:00:00 AM
[quoted text, click to view]

<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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Peter Bromberg [C# MVP]
4/30/2007 7:48:04 AM
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
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




[quoted text, click to view]
AddThis Social Bookmark Button