in my winforms app, I have a Queue which contains objects:
Queue<MyObj> _queuedRows = new Queue<MyObj>();
For each object, I must start a separate backgroundworker to do some timeconsuming job.
private void DoAll(List<MyObj> lst)
{
foreach (MyObj o in lst)
{
_queuedRows.Enqueue(o);
}
while (_queuedRows.Any())
DoSingle();
}
private void DoSingle()
{
if (!isCancelPending())
{
if (_queuedRows.Any())
{
MyObj currentObj = _queuedRows.Dequeue();
InitBackgroundWorker(currentObj);
}
}
}
private void InitBackgroundWorker(MyObj currentObj)
{
BackgroundWorker _worker = new BackgroundWorker();
_worker.WorkerSupportsCancellation = true;
_worker.WorkerReportsProgress = true;
_worker.DoWork += new DoWorkEventHandler(worker_DoWork);
_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
if (!_worker.IsBusy && currentObj != null)
_worker.RunWorkerAsync(currentObj);
}
My problem is, after the call to RunWorkerAsync, the execution jumps to the next iteration of the while (which is logical, as the worker is running async and it allows for the next iteration to happen).
What I actually need to do, is to tell the app somehow to WAIT until the backgroundworker has completed the job, and only then should it start the next iteration by continuing with calling DoSingle().
Should I apply a lock on the _queuedRows object or something similar? Thanks,