multiple backgroundworker queueing

我与影子孤独终老i 提交于 2020-01-04 09:23:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!