4

I have a thread that adds points onto a zedgraph component on a certain time interval. 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?

JensG
  • 13,148
  • 4
  • 45
  • 55
DazedNConfused
  • 189
  • 2
  • 13
  • Why do you create a new myThread-Object in every if-Statement? – Camo Apr 04 '16 at 08:59
  • The below link demonstrate the answer for your question. [Click Here(http://stackoverflow.com/a/2431028/6007610) – Anto Apr 04 '16 at 09:01
  • @Anto That's what he already has, but his Checkbox-Code is completly off. – Camo Apr 04 '16 at 09:02
  • Simply call `pause` and `play` directly, no need to spin up a thread for it. – Lasse V. Karlsen Apr 04 '16 at 09:09
  • (1) `if(checkBoxPause.Checked == true)` ==> if(checkBoxPause.Checked)` and `if(checkBoxPause.Checked == false)` ==> `if(! checkBoxPause.Checked)` or even better just `else` (2) the `catch` has no associaded `try` – JensG Apr 04 '16 at 11:48

1 Answers1

4

First you declare your myThread Object globally (only one!).

myThread heartGraph = new myThread()

Then you want to start your Worker-Method in a new Thread.

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

Now you can use your ManualResetEvent to Pause/Resume the work.

if (checkBoxPause.Checked == true) {
    //HRDataSummary.threadPause = true;
    checkBoxPause.Text = "Play >";
    heartGraph.pause();
} else { 
    //HRDataSummary.threadPause = false;
    checkBoxPause.Text = "Pause ||";
    heartGraph.play();
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
Camo
  • 1,122
  • 10
  • 15