小弟编写了一个多线程的程序用于学习多线程技术,现在遇到一个问题,就是通过主线程实现对工作线程进行暂停和恢复的功能,代码如下 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private static ManualResetEvent counterWait = new ManualResetEvent(false);
private static int n = 0;
Thread countexample;
private void start_Click(object sender, EventArgs e)
{
countexample = new Thread(new ThreadStart(count));
countexample.Start();
} private void count()
{
string t = "";
while(true)
{
if (!counterWait.WaitOne(1000, false))
{
t += n.ToString() + "\r\n";
SetText( t );
n++;
}
}
} private void suspended_Click(object sender, EventArgs e)
{
if (countexample.ThreadState == ThreadState.Running)
{
counterWait.Set();
return;
}
if (countexample.ThreadState == ThreadState.WaitSleepJoin)
{
//counterWait.Reset();
}
}#region 跨线程调用文本框(参考MSDN) // This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text); // This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly. private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}#endregion
}窗体上有三个控件:文本框(多行),两个按钮(启动按钮start和暂停按钮suspended),现在的问题是,点击启动按钮开始计数后,点击暂停按钮却停不下来,明明countexample.ThreadState == ThreadState.Running这句话为true,但是程序却不能进入if的这条语言中,小弟才开始学习多线程技术,请高手介绍下,使用这种事件机制如何完成线程间的互操作呀,小弟在此多谢了!
{
public Form1()
{
InitializeComponent();
} private static ManualResetEvent counterWait = new ManualResetEvent(false);
private static int n = 0;
Thread countexample;
private void start_Click(object sender, EventArgs e)
{
countexample = new Thread(new ThreadStart(count));
countexample.Start();
} private void count()
{
string t = "";
while(true)
{
if (!counterWait.WaitOne(1000, false))
{
t += n.ToString() + "\r\n";
SetText( t );
n++;
}
}
} private void suspended_Click(object sender, EventArgs e)
{
if (countexample.ThreadState == ThreadState.Running)
{
counterWait.Set();
return;
}
if (countexample.ThreadState == ThreadState.WaitSleepJoin)
{
//counterWait.Reset();
}
}#region 跨线程调用文本框(参考MSDN) // This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text); // This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly. private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}#endregion
}窗体上有三个控件:文本框(多行),两个按钮(启动按钮start和暂停按钮suspended),现在的问题是,点击启动按钮开始计数后,点击暂停按钮却停不下来,明明countexample.ThreadState == ThreadState.Running这句话为true,但是程序却不能进入if的这条语言中,小弟才开始学习多线程技术,请高手介绍下,使用这种事件机制如何完成线程间的互操作呀,小弟在此多谢了!
while(true)
{
if (!counterWait.WaitOne(1000, false))
{
t += n.ToString() + "\r\n";
SetText( t );
n++;
}
Thread.Sleep(100);
}
另外,counterWait.WaitOne执行的时候Thread.ThreadState 是WaitSleepJoin
如果为 true,则将初始状态设置为终止;如果为 false,则将初始状态设置为非终止。非终止状态,导致线程阻止。你的线程应该一开始就处于阻止状态吧。
改
if (counterWait.WaitOne(1000, false))
我试过了,应该设为false呀,否则不会进入if语句的!
肯定不是这个问题,这句话是为了suspendend中的set()和reset()服务的,这个情况下,肯定是
if (!counterWait.WaitOne(1000, false))
才对呀!
多谢你的提醒呀,我今天通过引入一个判断符实现的我需要的功能了,但是如果大家有更好的建议,希望大家告诉我呀,小弟才开始接触多线程问题,希望大家不吝赐教才好。
代码如下public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private static ManualResetEvent counterWait = new ManualResetEvent(false);
private static int n = 0;
Thread countexample;
bool suspendendOrContinue = true;//true为暂停,false为继续
private void start_Click(object sender, EventArgs e)
{
countexample = new Thread(new ThreadStart(count));
countexample.Start();
} private void count()
{
string t = "";
while(true)
{
if (!counterWait.WaitOne(1000, false))
{
t += n.ToString() + "\r\n";
SetText( t );
n++;
}
Thread.Sleep(100);
}
} private void suspendend_Click(object sender, EventArgs e)
{
if (suspendendOrContinue)
{
counterWait.Set();
}
if (!suspendendOrContinue)
{
counterWait.Reset();
}
suspendendOrContinue = !suspendendOrContinue;
}