R
rbDeveloper
The following is from a simple Windows application in VS2005, which has
button1 and textbox1 dragged onto a form.
In StartThreads(), I call ThreadPool.QueueUserWorkItem(), then call
WaitOne(). My expectation is that I would see the text generated in
WasteTime() before seeing the "Hey" printout that comes after WaitOne().
Instead, I'm seeing the "Hey" as the first thing to print out in the text
box.
Any thoughts on why WaitOne() isn't waiting for the ThreadPool thread to
complete?
Thanks...
private void button1_Click(object sender, EventArgs e)
{
StartThreads();
}
private void StartThreads()
{
CalculationRequest cr = new CalculationRequest();
cr.UserID = "42";
ThreadPool.QueueUserWorkItem(new WaitCallback(WasteTime), cr);
cr.ProcessingEvent.WaitOne();
textBox1.Text += "Hey";
textBox1.Text += "\r\n";
}
private void WasteTime(object state)
{
if (state is CalculationRequest)
{
CalculationRequest cr = state as CalculationRequest;
cr.ProcessingEvent.Set();
for (int i = 0; i < 5; i++)
{
SetText(String.Format("threadId: {0}, count: {1}",
cr.UserID, i));
System.Threading.Thread.Sleep(100);
}
}
}
//The following allows us to set text in textbox1 from a
//ThreadPool thread.
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text += String.Format("{0}\n", text);
this.textBox1.Text += "\r\n";
}
}
class CalculationRequest
{
public string UserID;
// Thead/Sync info
public ManualResetEvent ProcessingEvent = new
ManualResetEvent(false);
}
button1 and textbox1 dragged onto a form.
In StartThreads(), I call ThreadPool.QueueUserWorkItem(), then call
WaitOne(). My expectation is that I would see the text generated in
WasteTime() before seeing the "Hey" printout that comes after WaitOne().
Instead, I'm seeing the "Hey" as the first thing to print out in the text
box.
Any thoughts on why WaitOne() isn't waiting for the ThreadPool thread to
complete?
Thanks...
private void button1_Click(object sender, EventArgs e)
{
StartThreads();
}
private void StartThreads()
{
CalculationRequest cr = new CalculationRequest();
cr.UserID = "42";
ThreadPool.QueueUserWorkItem(new WaitCallback(WasteTime), cr);
cr.ProcessingEvent.WaitOne();
textBox1.Text += "Hey";
textBox1.Text += "\r\n";
}
private void WasteTime(object state)
{
if (state is CalculationRequest)
{
CalculationRequest cr = state as CalculationRequest;
cr.ProcessingEvent.Set();
for (int i = 0; i < 5; i++)
{
SetText(String.Format("threadId: {0}, count: {1}",
cr.UserID, i));
System.Threading.Thread.Sleep(100);
}
}
}
//The following allows us to set text in textbox1 from a
//ThreadPool thread.
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text += String.Format("{0}\n", text);
this.textBox1.Text += "\r\n";
}
}
class CalculationRequest
{
public string UserID;
// Thead/Sync info
public ManualResetEvent ProcessingEvent = new
ManualResetEvent(false);
}