使用 winforms c# 更新进度条

发布于 2024-08-29 11:01:27 字数 1454 浏览 1 评论 0原文

我的模块中有一个功能,用户可以 扫描系统中的串口数量,当 用户单击“自动扫描”按钮,代码将被删除 通过每个串口并发送测试消息并等待 以便回复。

我正在使用进度条控件来显示自动扫描的过程。 为此,我需要将代码中的值传递给“x”和“Y” 更新吧。由于我的代码已经是,我如何传递该值 在 foreach 循环中获取串行端口。

Y = 应传递串口总数的值 X = 应该迭代每个端口并传递值

希望我清楚 req.

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string strAckData = "";

        foreach (SerialPort sp in comPortsList)
        {
            sp.Open();
            string sendData = "Auto scan";
            sp.Write(sendData);
            strAckData += "Connection live on port " + sp.ReadExisting() + "\n";
            sp.Close();

            double dIndex = (double)x; **//How to pass the value here ?**
            double dTotal = (double)y; **//How to pass the value here ?**
            double dProgressPercentage = (dIndex / dTotal);
            int iProgressPercentage = (int)(dProgressPercentage * 100);

            // update the progress bar
            backgroundWorker1.ReportProgress(iProgressPercentage);

        }
        richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text = strAckData; }));
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressBar.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        StatusLabel.Text = "Auto Scan completed";
    } 

There is a functionality in my module, where the user can
scan the number of serial ports in the system and when the
user clicks "Auto scan" button, the code will have to go
through each serial port and send a test message and wait
for the reply.

I am using Progress bar control to show process of autoscan.
For which i need to pass the value to "x" and "Y" in my code to
update the bar. How can i pass the value since my code is already
in a foreach loop for getting the serialports.

Y = should pass the value of total number of serial ports
X = should iterate through each port and pass the value

Hope i am clear with req.

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string strAckData = "";

        foreach (SerialPort sp in comPortsList)
        {
            sp.Open();
            string sendData = "Auto scan";
            sp.Write(sendData);
            strAckData += "Connection live on port " + sp.ReadExisting() + "\n";
            sp.Close();

            double dIndex = (double)x; **//How to pass the value here ?**
            double dTotal = (double)y; **//How to pass the value here ?**
            double dProgressPercentage = (dIndex / dTotal);
            int iProgressPercentage = (int)(dProgressPercentage * 100);

            // update the progress bar
            backgroundWorker1.ReportProgress(iProgressPercentage);

        }
        richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text = strAckData; }));
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressBar.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        StatusLabel.Text = "Auto Scan completed";
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不放开 2024-09-05 11:01:27

您可以从 comPortsList 变量中获取端口数。那么索引只是递增循环变量的问题:

double dTotal = (double)(comPortsList.Count);
double dIndex = 0;

foreach (SerialPort sp in comPortsList)
{
  // talk to serial port as at the moment

  dIndex = dIndex + 1;  // or ++dIndex to be more concise
  double dProgressPercentage = dIndex / dTotal;
  // etc.
}

You can get the number of ports from your comPortsList variable. Then the index is just a matter of incrementing a loop variable:

double dTotal = (double)(comPortsList.Count);
double dIndex = 0;

foreach (SerialPort sp in comPortsList)
{
  // talk to serial port as at the moment

  dIndex = dIndex + 1;  // or ++dIndex to be more concise
  double dProgressPercentage = dIndex / dTotal;
  // etc.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文