简体   繁体   English

暂停并恢复线程活动

[英]Pause and resume thread activity

I have a thread that adds points onto a zedgraph component on a certain time interval. 我有一个线程,在某个时间间隔内将点添加到zedgraph组件上。 I need to pause the adding of the points on pressing a check box and then resume adding them when the checkbox is pressed again. 我需要在按下复选框时暂停添加点,然后在再次按下该复选框时继续添加它们。 The following is what I have for thread: 以下是我对线程的看法:

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();
    }
}

And then, I have called for the play and pause thread from a checkbox. 然后,我从复选框中调用了播放和暂停线程。

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();
            }
        }

What am I missing? 我错过了什么? Or is it completely wrong use of ManualResetEvent? 或者是否完全错误地使用了ManualResetEvent?

First you declare your myThread Object globally (only one!). 首先,您全局声明myThread对象(只有一个!)。

myThread heartGraph = new myThread()

Then you want to start your Worker-Method in a new Thread. 然后你想在一个新线程中启动你的Worker-Method。

Thread worker = new Thread(heartGraph.threadHeartrateGraph);
worker.Start();

Now you can use your ManualResetEvent to Pause/Resume the work. 现在,您可以使用ManualResetEvent暂停/恢复工作。

if (checkBoxPause.Checked == true) {
    //HRDataSummary.threadPause = true;
    checkBoxPause.Text = "Play >";
    heartGraph.pause();
} else { 
    //HRDataSummary.threadPause = false;
    checkBoxPause.Text = "Pause ||";
    heartGraph.play();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM