Hi thanks for the reply, I have adapted the code for asynchronous programming.
I created a list of backgrounworkers each associated with a serial port. I created a function to open all and close all but one in particular I want to open or close one in particular as not locate within the processes that are running. Can you make a process with a value, or a value to pass BackgroundWorker and go all ask for?
private List<backgroundworker> _workers = new List<backgroundworker>();
......
public void OpenAll()
{
string[] portNames = SerialPort.GetPortNames();
foreach (string name in portNames)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
_workers.Add(bw);
bw.RunWorkerAsync(name);
}
}
</backgroundworker></backgroundworker>
Another problem I have is that by canceling all processes stuck me here (while (bw.IsBusy){}:
public void CancelAllWorkers()
{
foreach (BackgroundWorker bw in _workers)
{
if (bw.IsBusy)
{
bw.CancelAsync();
while (bw.IsBusy) { }
bw.DoWork -= new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.Dispose();
_workers.Remove(bw);
}
}
}