C# 使用WinC窗体更新进度条#
我的模块中有一个功能,用户可以在其中 扫描系统中的串行端口数以及 用户点击“自动扫描”按钮,代码将不得不消失 通过每个串行端口发送测试消息并等待 请回答 我正在使用进度条控件显示自动扫描的过程。 为此,我需要将代码中的值传递给“x”和“Y” 更新栏。既然我的代码已经存在,我如何传递该值 在foreach循环中获取串行端口 Y=应传递串行端口总数的值 X=应遍历每个端口并传递值 希望我清楚地知道C# 使用WinC窗体更新进度条#,c#,serial-port,C#,Serial Port,我的模块中有一个功能,用户可以在其中 扫描系统中的串行端口数以及 用户点击“自动扫描”按钮,代码将不得不消失 通过每个串行端口发送测试消息并等待 请回答 我正在使用进度条控件显示自动扫描的过程。 为此,我需要将代码中的值传递给“x”和“Y” 更新栏。既然我的代码已经存在,我如何传递该值 在foreach循环中获取串行端口 Y=应传递串行端口总数的值 X=应遍历每个端口并传递值 希望我清楚地知道 private void backgroundWorker1_DoWork(object sende
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";
}
您可以从
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.
}
@伊托尔森,更清楚地说。。Y应该是可用端口总数的值,X应该是当时打开的端口的值。在这种情况下,X不是一个常量值。每次代码执行sp.open()时,X的值都会发生变化。为了反映这一点,我更新了答案。对不起,误会了。我仍然不确定我是否完全理解了你所说的港口的“价值”——如果我仍然偏离了航向,请道歉。