Show progressBar / Wait dialog while receiving serial data
How can I show a progress bar (not percentage just neverending bar) or a wait dialog while action in the _DataReceived is performed?
e.g.:
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);
开发者_如何学Python }
}
}
Just add a progress bar, set its Style
to ProgressBarStyle.Marquee
, so it indicates that it doesn't give a percentage. Now increment its value as follows:
(I assume this code is in a Form
, otherwise, if you are using a BackgroundWorker
, use its ReportProgress
method)
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;
}
}
}
精彩评论