C# 开始暂停并恢复在图形上打印串行数据

C# 开始暂停并恢复在图形上打印串行数据,c#,winforms,serial-port,zedgraph,C#,Winforms,Serial Port,Zedgraph,windows窗体中是否有允许我暂停正在进行的串行数据接收过程的控件,因为我需要检查和验证正在绘制在图形上的数据。检查完毕后,我需要恢复该过程。这就像是一个开始。。暂停..继续。。暂停过程 如果有人能为我推荐上述的理想程序,那就太好了。 后台工作程序是实现此功能的唯一方法吗 根据您使用的协议,您可能可以指示发送方在暂停时不要发送任何内容,但我认为最直接的方法是将传入数据缓冲在队列或简单数组上,然后,在用户处于暂停状态时,不使用新数据更新屏幕。我执行此任务的方法: 实际上,您需要使用线程的原因只有

windows窗体中是否有允许我暂停正在进行的串行数据接收过程的控件,因为我需要检查和验证正在绘制在图形上的数据。检查完毕后,我需要恢复该过程。这就像是一个开始。。暂停..继续。。暂停过程

如果有人能为我推荐上述的理想程序,那就太好了。
后台工作程序是实现此功能的唯一方法吗

根据您使用的协议,您可能可以指示发送方在暂停时不要发送任何内容,但我认为最直接的方法是将传入数据缓冲在队列或简单数组上,然后,在用户处于暂停状态时,不使用新数据更新屏幕。

我执行此任务的方法:

实际上,您需要使用线程的原因只有一个,那就是组织时间 例:

错:

现在让我们来做你的主要任务暂停/恢复

您需要定义一个bool标志来决定数据采集/绘图循环:

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

由于数据是大量的,如果我必须将它们存储在阵列/队列中,直到端口再次关闭,以便在我恢复后接收更多数据,这不是一个问题吗?我不明白为什么。数据丢失不是一种选择吗?您从哪种设备/系统获取数据?我习惯于从serialPort打印大量数据,但我总是控制我的设备:我向serialPort发送命令,然后接收数据,然后打印,如果需要暂停,我停止发送命令。数据来自传感器,传感器仅在向其发送特定命令时才发送数据。当我单击windows窗体上的开始按钮时,数据正在图形上绘制。但是,我的要求是我放置一个暂停按钮,暂时停止程序并查看图表中的情况,然后单击开始按钮继续。我将如何实现这一点?
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();
}
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;
}
}