The following is from a simple Windows application in VS2005, which has
button1 and textbox1 dragged onto a form.
In StartThreads(), I call ThreadPool.Queu eUserWorkItem() , 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(o bject sender, EventArgs e)
{
StartThreads();
}
private void StartThreads()
{
CalculationRequ est cr = new CalculationRequ est();
cr.UserID = "42";
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Wa steTime), cr);
cr.ProcessingEv ent.WaitOne();
textBox1.Text += "Hey";
textBox1.Text += "\r\n";
}
private void WasteTime(objec t state)
{
if (state is CalculationRequ est)
{
CalculationRequ est cr = state as CalculationRequ est;
cr.ProcessingEv ent.Set();
for (int i = 0; i < 5; i++)
{
SetText(String. Format("threadI d: {0}, count: {1}",
cr.UserID, i));
System.Threadin g.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.T ext += String.Format(" {0}\n", text);
this.textBox1.T ext += "\r\n";
}
}
class CalculationRequ est
{
public string UserID;
// Thead/Sync info
public ManualResetEven t ProcessingEvent = new
ManualResetEven t(false);
}
button1 and textbox1 dragged onto a form.
In StartThreads(), I call ThreadPool.Queu eUserWorkItem() , 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(o bject sender, EventArgs e)
{
StartThreads();
}
private void StartThreads()
{
CalculationRequ est cr = new CalculationRequ est();
cr.UserID = "42";
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Wa steTime), cr);
cr.ProcessingEv ent.WaitOne();
textBox1.Text += "Hey";
textBox1.Text += "\r\n";
}
private void WasteTime(objec t state)
{
if (state is CalculationRequ est)
{
CalculationRequ est cr = state as CalculationRequ est;
cr.ProcessingEv ent.Set();
for (int i = 0; i < 5; i++)
{
SetText(String. Format("threadI d: {0}, count: {1}",
cr.UserID, i));
System.Threadin g.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.T ext += String.Format(" {0}\n", text);
this.textBox1.T ext += "\r\n";
}
}
class CalculationRequ est
{
public string UserID;
// Thead/Sync info
public ManualResetEven t ProcessingEvent = new
ManualResetEven t(false);
}
Comment