在接收串行数据时显示进度条/等待对话框
本文关键字:对话框 等待 显示 数据 | 更新日期: 2023-09-27 18:10:43
我怎么能显示一个进度条(不是百分比只是永无止境的条)或等待对话框,而在_DataReceived的操作执行?
例如:
private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
while (sp.BytesToRead > 1)
{
string line = sp.ReadLine().Trim();
if (line == "EOC")
{
//finish
}
else
{
//string data = sp.ReadExisting();
_serialBuffer.Enqueue(line);
}
}
}
只需添加一个进度条,将其Style
设置为ProgressBarStyle.Marquee
,因此它表示它不给出百分比。现在按如下方式增加它的值:(我假设此代码在Form
中,否则,如果您使用BackgroundWorker
,请使用其ReportProgress
方法)
private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
while (sp.BytesToRead > 1)
{
string line = sp.ReadLine().Trim();
if (line == "EOC")
{
//finish
this.progressBar1.Visible = false;
}
else
{
//string data = sp.ReadExisting();
_serialBuffer.Enqueue(line);
if (this.progressBar1.Value < 100)
this.progressBar1.Value++;
else
this.progressBar1.Value = 0;
}
}
}