开发者

Show progressBar / Wait dialog while receiving serial data

开发者 https://www.devze.com 2023-04-11 15:06 出处:网络
How can I show a progress bar (not percentage just neverending bar) or a wait dialog while action in the _DataReceived is performed?

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

     }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号