如何等待后台工作者完成处理?

时间:2011-10-21 08:14:06

标签: c# winforms backgroundworker parallel-processing

我有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
   }

5 个答案:

答案 0 :(得分:5)

根据Renuiz的回答,我会这样做:

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();
        }
    }
}

答案 1 :(得分:4)

也许你可以设置和检查后台工作者完整事件处理程序中的标志。例如:

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();
}

答案 2 :(得分:4)

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

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 Forms

答案 3 :(得分:1)

您可以将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);
        }

    }
}

答案 4 :(得分:0)

考虑使用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响应。