如何等待后台worker完成处理

本文关键字:worker 处理 后台 何等待 等待 | 更新日期: 2023-09-27 18:12:01

我有3个后台工作人员,每个工作人员处理一个24位位图图像(Y, Cb, Cr)的通道。每个8位图像的处理需要几秒钟,它们可能不会同时完成。

我想合并通道回到一个图像当我完成。当单击按钮时,每个backgroundWorkerN.RunWorkerAsync()都启动,当它们完成时,我将标记设置为true。我尝试使用while循环while (!y && !cb && !cr) { }不断检查标志,直到它们为真,然后退出循环并继续处理下面的代码,这是将通道合并在一起的代码。但是,当我运行它时,进程却永远不会结束。

   private void button1_Click(object sender, EventArgs e)
   {
        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.RunWorkerAsync();
        backgroundWorker3.RunWorkerAsync();
        while (!y && !cb && !cr) { }
        //Merge Code
   }

如何等待后台worker完成处理

基于reniz的回答,我将这样做:

private object lockObj;
private void backgroundWorkerN_RunWorkerCompleted(
    object sender, 
    RunWorkerCompletedEventArgs e)
{
    lock (lockObj)
    {
        y = true;
        if (cb && cr) // if cb and cr flags are true - 
                      // other backgroundWorkers finished work
        {
            someMethodToDoOtherStuff();
        }
    }
}

也许你可以在后台完成事件处理程序中设置和检查标志。例如:

private void backgroundWorkerN_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    y = true;
    if(cb && cr)//if cb and cr flags are true - other backgroundWorkers finished work
       someMethodToDoOtherStuff();
}

我将使用三个线程而不是后台工作线程。

using System.Threading;
class MyConversionClass
{
    public YCBCR Input;
    public RGB Output
    private Thread Thread1;
    private Thread Thread2;
    private Thread Thread3;
    private int pCompletionCount;
    public MyConversionClass(YCBCR myInput, RGB myOutput)
    {
        this.Input = myInput;
        this.Output = myOutput;
        this.Thread1 = new Thread(this.ComputeY);
        this.Thread2 = new Thread(this.ComputeCB);
        this.Thread3 = new Thread(this.ComputeCR);
    }
    public void Start()
    {
        this.Thread1.Start();
        this.Thread2.Start();
        this.Thread3.Start();
    }
    public void WaitCompletion()
    {
        this.Thread1.Join();
        this.Thread2.Join();
        this.Thread3.Join();
    }
    // Call this method in background worker 1
    private void ComputeY()
    {
        // for each pixel do My stuff
        ...
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    // Call this method in background worker 2
    private void ComputeCB()
    {
        // for each pixel do My stuff
        ...
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    // Call this method in background worker 3
    private void ComputeCR()
    {
        // for each pixel do My stuff
        ...
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    private void MergeTogether()
    {
        // We merge the three channels together
        ...
    }
}

现在在你的代码中你只需这样做:

private void button1_Click(object sender, EventArgs e)
{
    MyConversionClass conversion = new MyConversionClass(myinput, myoutput);
    conversion.Start();
    conversion.WaitCompletion();
    ... your other stuff
}

但是这将暂停您的GUI,直到所有操作完成。我将使用SynchronizationContext来通知GUI操作已经完成。

这个版本使用SynchronizationContext来同步GUI线程而不需要等待。这将保持GUI响应,并在其他线程中执行整个转换操作。

using System.Threading;
class MyConversionClass
{
    public YCBCR Input;
    public RGB Output
    private EventHandler Completed;
    private Thread Thread1;
    private Thread Thread2;
    private Thread Thread3;
    private SynchronizationContext SyncContext;
    private volatile int pCompletionCount;
    public MyConversionClass()
    {
        this.Thread1 = new Thread(this.ComputeY);
        this.Thread2 = new Thread(this.ComputeCB);
        this.Thread3 = new Thread(this.ComputeCR);
    }
    public void Start(YCBCR myInput, RGB myOutput, SynchronizationContext syncContext, EventHandler completed)
    {
        this.SyncContext = syncContext;
        this.Completed = completed;
        this.Input = myInput;
        this.Output = myOutput;
        this.Thread1.Start();
        this.Thread2.Start();
        this.Thread3.Start();
    }
    // Call this method in background worker 1
    private void ComputeY()
    {
        ... // for each pixel do My stuff
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    // Call this method in background worker 2
    private void ComputeCB()
    {
        ... // for each pixel do My stuff
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    // Call this method in background worker 3
    private void ComputeCR()
    {
        ... // for each pixel do My stuff
        if (Interlocked.Increment(ref this.CompletionCount) == 3)
            this.MergeTogether();
    }
    private void MergeTogether()
    {
        ... // We merge the three channels together
        // We finish everything, we can notify the application that everything is completed.
        this.syncContext.Post(RaiseCompleted, this);
    }
    private static void RaiseCompleted(object state)
    {
        (state as MyConversionClass).OnCompleted(EventArgs.Empty);
    }
    // This function is called in GUI thread when everything completes.
    protected virtual void OnCompleted(EventArgs e)
    {
        EventHandler completed = this.Completed;
        this.Completed = null;
        if (completed != null)
            completed(this, e);
    }
}

现在,在你的代码中…

private void button1_Click(object sender, EventArgs e)
{
    button1.Enabled = false;
    MyConversionClass conversion = new MyConversionClass();
    conversion.Start(myinput, myoutput, SynchronizationContext.Current, this.conversion_Completed);
}
private void conversion_Completed(object sender, EventArgs e)
{
    var output = (sender as MyConversionClass).Output;
    ... your other stuff that uses output
    button1.Enabled = true;
}

这两种方法的优点是它们与GUI无关,您可以将它们放在库中,并使您宝贵的多线程转换代码完全独立于您正在使用的GUI,即WPF, Web或Windows窗体。

您可以将WaitHandle.WaitAllEventWaitHandle结合使用以实现您所需要的。这里附上了一个代码样本,它做了我提到的。所包含的代码只是解决方案外观的大纲。您必须添加适当的异常处理和防御方法,以使此代码更稳定。

using System;
using System.ComponentModel;
using System.Threading;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            BWorkerSyncExample sample = new BWorkerSyncExample();
            sample.M();
        }
    }
    class BWorkerSyncExample
    {
        BackgroundWorker worker1, worker2, worker3;
        EventWaitHandle[] waithandles;
        public void M()
        {
            Console.WriteLine("Starting background worker threads");
            waithandles = new EventWaitHandle[3];
            waithandles[0] = new EventWaitHandle(false, EventResetMode.ManualReset);
            waithandles[1] = new EventWaitHandle(false, EventResetMode.ManualReset);
            waithandles[2] = new EventWaitHandle(false, EventResetMode.ManualReset);
            StartBWorkerOne();
            StartBWorkerTwo();
            StartBWorkerThree();
            //Wait until all background worker complete or timeout elapse
            Console.WriteLine("Waiting for workers to complete...");
            WaitHandle.WaitAll(waithandles, 10000);
            Console.WriteLine("All workers finished their activities");
            Console.ReadLine();
        }
        void StartBWorkerThree()
        {
            if (worker3 == null)
            {
                worker3 = new BackgroundWorker();
                worker3.DoWork += (sender, args) =>
                                    {
                                        M3();
                                        Console.WriteLine("I am done- Worker Three");
                                    };
                worker3.RunWorkerCompleted += (sender, args) =>
                                    {
                                        waithandles[2].Set();
                                    };
            }
            if (!worker3.IsBusy)
                worker3.RunWorkerAsync();
        }
        void StartBWorkerTwo()
        {
            if (worker2 == null)
            {
                worker2 = new BackgroundWorker();
                worker2.DoWork += (sender, args) =>
                                       {
                                           M2();
                                           Console.WriteLine("I am done- Worker Two");
                                       };
                worker2.RunWorkerCompleted += (sender, args) =>
                                       {
                                           waithandles[1].Set();
                                       };
            }
            if (!worker2.IsBusy)
                worker2.RunWorkerAsync();
        }
        void StartBWorkerOne()
        {
            if (worker1 == null)
            {
                worker1 = new BackgroundWorker();
                worker1.DoWork += (sender, args) =>
                                       {
                                           M1();
                                           Console.WriteLine("I am done- Worker One");
                                       };
                worker1.RunWorkerCompleted += (sender, args) =>
                                       {
                                           waithandles[0].Set();
                                       };
            }
            if (!worker1.IsBusy)
                worker1.RunWorkerAsync();
        }
        void M1()
        {
           //do all your image processing here.
        //simulate some intensive activity.
        Thread.Sleep(3000);
        }
        void M2()
        {
          //do all your image processing here.
        //simulate some intensive activity.
        Thread.Sleep(1000);
        }
        void M3()
        {
         //do all your image processing here.
        //simulate some intensive activity.
        Thread.Sleep(4000);
        }
    }
}

考虑使用AutoResetEvents:

private void button1_Click(object sender, EventArgs e)
    {
        var e1 = new System.Threading.AutoResetEvent(false);
        var e2 = new System.Threading.AutoResetEvent(false);
        var e3 = new System.Threading.AutoResetEvent(false);
        backgroundWorker1.RunWorkerAsync(e1);
        backgroundWorker2.RunWorkerAsync(e2);
        backgroundWorker3.RunWorkerAsync(e3);

        // Keep the UI Responsive
        ThreadPool.QueueUserWorkItem(x =>
        {
            // Wait for the background workers
            e1.WaitOne();
            e2.WaitOne();
            e3.WaitOne();
            MethodThatNotifiesIamFinished();
        });
        //Merge Code
    }

    void BackgroundWorkerMethod(object obj)
    {
        var evt = obj as AutoResetEvent;
        //Do calculations
        etv.Set();
    }

这样你就不会在某些循环中浪费cpu时间&使用一个单独的线程来等待,可以保持UI响应。