How to wait for background worker to finish processing?

前端 未结 5 694
闹比i
闹比i 2021-01-19 17:07

I have 3 background workers each processing a channel of a 24-bit Bitmap image (Y, Cb, Cr). The processing for each 8-bit image takes several seconds and they might not com

5条回答
  •  天命终不由人
    2021-01-19 17:50

    You can use WaitHandle.WaitAll in conjunction with EventWaitHandle to achieve what you need. Herein enclosed a code sample which does what I mentioned. The enclosed code is just an outline of how the solution will look like. You must add proper exception handling and defensive approach to make this code more stable.

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

提交回复
热议问题