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