My way to do such task:
actually you need to use thread for only one reason, which is to organize time
EX:
WRONG:
while(true){
GetDataFromSerialPort(); // you don't know how long it takes 10ms, 56ms, 456ms ...?
DrawData(); // this plots data at randomly spaced intervals
}
RIGHT
while(true){
Thread th1 = new Thread(new ThreadStart(GetDataFromSerialPort)); // thread to acquire
th1.IsBackground = true; // new data
th1.Start();
wait(100); // main thread waits few milliseconds
Thread th2 = new Thread(new ThreadStart(DrawData)); // draw on zedGraph on other thread
th2.IsBackground = true;
th2.Start();
}
Now let's do your main quastion (pause/resume...)
you need to define a bool flag that decides your data acquisition/drawing loop:
bool isRunning = false; // initially it's stopped
public void startDrawing()
{
isRunning = true;
while(isRunning)
{
//thread to get data
//wait
//thread to draw it
//refer to the above "right" example
}
}
// Now let's set buttons work
private void button1_Click(object sender, EventArgs e)
{
if(button1.text == "START" || button1.text == "RESUME")
{
button1.text = "PAUSE";
startDrawing();
}
else
{
button.text = "RESUME";
isRunning = false;
}
}