我有一个线程,在某个时间间隔内将点添加到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?
答案 0 :(得分:4)
首先,您全局声明List<Person> persons = repository.findAll(new PersonSpec(personSpec));
对象(只有一个!)。
myThread
然后你想在一个新线程中启动你的Worker-Method。
myThread heartGraph = new myThread()
现在您可以使用ManualResetEvent暂停/恢复工作。
Thread worker = new Thread(heartGraph.threadHeartrateGraph);
worker.Start();