我有一个线程,可以在一定时间间隔内将点添加到zedgraph组件上。我需要在按下复选框时暂停添加点,然后在再次按下复选框时恢复添加点。以下是我对线程的了解:
public class myThread{
ManualResetEvent pauseResumeThread = new ManualResetEvent(true);
public void threadHeartrateGraph()
{
for (int k = 15; k < _hr.Length; k++)
{
pauseResumeThread.WaitOne();
if (HRDataSummary.threadPause == true)
{
break;
}
x = k;
y = _hr[k];
list1.Add(x, y);
_displayHRGraph.Invoke(list1, graph_HeartRate, _GraphName[0]);
graph_HeartRate.XAxis.Scale.Min = k-14;
graph_HeartRate.XAxis.Scale.Max = k+1;
Thread.Sleep(_interval * 1000);
}
}
catch (NullReferenceException)
{
}
}
public void play()
{
pauseResumeThread.Set();
}
public void pause()
{
pauseResumeThread.Reset();
}
}
然后,我从复选框要求播放和暂停线程。
private void checkBoxPause_CheckedChanged(object sender, EventArgs e)
{
if(checkBoxPause.Checked == true)
{
//HRDataSummary.threadPause = true;
checkBoxPause.Text = "Play >";
myThread mythread = new myThread();
Thread pause = new Thread(mythread.pause);
pause.Start();
}
if (checkBoxPause.Checked == false)
{
//HRDataSummary.threadPause = false;
checkBoxPause.Text = "Pause ||";
myThread mythread = new myThread();
Thread play = new Thread(mythread.play);
play.Start();
}
}
我想念什么?还是使用ManualResetEvent完全错误?
首先,您myThread
全局声明对象(只有一个!)。
myThread heartGraph = new myThread()
然后,您想在新线程中启动Worker-Method。
Thread worker = new Thread(heartGraph.threadHeartrateGraph);
worker.Start();
现在,您可以使用ManualResetEvent暂停/恢复工作。
if (checkBoxPause.Checked == true) {
//HRDataSummary.threadPause = true;
checkBoxPause.Text = "Play >";
heartGraph.pause();
} else {
//HRDataSummary.threadPause = false;
checkBoxPause.Text = "Pause ||";
heartGraph.play();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句