接收串行数据时显示progressBar / Wait对话框

时间:2011-10-09 17:49:10

标签: c# winforms user-interface serial-port

如果在执行_DataReceived中的操作时,如何显示进度条(不是百分比只是无休止的栏)或等待对话框?

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

 }

1 个答案:

答案 0 :(得分:1)

只需添加一个进度条,将其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;
                        }
              }

     }