具有返回值的生产者消费者模式

时间:2018-08-11 13:29:35

标签: c#

我想实现一个带有返回值的生产者消费者模式。我没有使用-default arhitecture -default toolchain -uclib -enable c++ -enable compiler link time opt support -enable mmu support sys config -enable root login -enable busybox default shell -disable getty -enable remount root -disable network interfaces kernel -enable linux kernel target packages -install watchdog daemon -enable zip hardware handling -enable rpi-userland -enable start vcfield graphic -enable qt5 -enable gui module -enable widgets module -enable opengl support -enable opengl module -enable eglfs -default graphical platform(eglfs) -enable qtbase,fontconfig,gif,png,jpg,dbus,icu,tslib,qt5 declarative,qt5graphicaleffects,qt5imageformatting,qtquickcontrols,qtquickcontrols2,qt5script,qt5sensors,qt5serialports,qt5xmlpatterns ,而是使用了一种更原始​​的方法..它具有Task.Result属性,并利用ResultManualResetEvent.Wait()来确保{{1 }}已设置。

我的实现的利弊是什么?谢谢。

ManualResetEvent.Set()

1 个答案:

答案 0 :(得分:0)

我喜欢使用后台工作者:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static  List<Job> Queue = new List<Job>();

        const int TOTAL_JOB_COUNT = 100;

        const int TOTAL_WORKER_THREAD = 2;

        public static ManualResetEvent manualResetEvent = new ManualResetEvent(false);

        static void Main(string[] args)
        {

            for (int i = 0; i < TOTAL_JOB_COUNT; i++)
            {
                Job newJob = new Job();
                Object thisLock = new Object();
                lock (thisLock)
                {
                    Queue.Add(newJob);
                }
                newJob.state.id = i;
                newJob.ProgressChanged += new ProgressChangedEventHandler(newJob_ProgressChanged);
                newJob.DoWork +=new DoWorkEventHandler(newJob.doWork);
                newJob.RunWorkerAsync();
            }
            manualResetEvent.WaitOne();
            Console.WriteLine("All jobs completed");
            Console.ReadLine();
        }
        static void newJob_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Job job = sender as Job;
            State state = job.state;
            Console.WriteLine("Job Completed,  ID: '{0}', Results = '{1}'", state.id, state.Result);
            Object thisLock = new Object();
            {
                for (int i = 0; i < Queue.Count; i++)
                {
                    if (state.id == Queue[i].state.id)
                    {
                        Queue.RemoveAt(i);
                        break;
                    }
                }
                if (Queue.Count == 0) manualResetEvent.Set();
            }

        }

    }
    public class Job : BackgroundWorker
    {
        public State state = new State();
        public Job()
        {
            this.WorkerReportsProgress = true;
        }
        public void doWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(1000);
            state.Result = 123;
            this.ReportProgress(100, state);
            this.Dispose();
        }
    }
    public class State
    {
        public int id;
        public int Result { get; set; }
    }
}