问题
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,
回答1:
Instead of calling DoSingle
in the while loop, change it to call DoSingle
once and then in the background worker's RunWorkerCompleted
event handler call DoSingle
again and again until the queue is done.
private void DoAll(List<MyObj> lst)
{
foreach (MyObj o in lst)
{
_queuedRows.Enqueue(o);
}
if (_queuedRows.Any())
DoSingle();
}
Also since you're not processing all objects in the queue in parallel, instantiate background worker only once and reuse it.
回答2:
You must use only one backgroundworker.
来源:https://stackoverflow.com/questions/11145938/multiple-backgroundworker-queueing