Ejemplo n.º 1
0
 public Consumer(string id, IWorkQueue WorkQueue, ConstituencyList constitlist, ProgManager progManager)
 {
     this.id          = id;
     complete         = false;
     this.workQueue   = WorkQueue;
     this.constitlist = constitlist;
     this.progMan     = progManager;
     (T = new Thread(run)).Start();
     ThreadsRunning++;
 }
Ejemplo n.º 2
0
        public void RunProducerConsumer()
        {
            constituencylist = new ConstituencyList();

            ProgManager progMan = new ProgManager(dataConfig.recConfig.Count);

            lblProcess.Text = "Creating and starting all producers and consumers";

            var workQueue = new WorkQueue(4);

            Producer[] producers = { new Producer("P1", workQueue, dataConfig, IOhandler),
                                     new Producer("P2", workQueue, dataConfig, IOhandler) };

            Consumer[] consumers = { new Consumer("C1", workQueue, constituencylist, progMan),
                                     new Consumer("C2", workQueue, constituencylist, progMan) };

            while (progMan.ItemsLeft > 0)
            {
                ;
            }

            lblProcess.Text = "Shutting down all producers and consumers";

            workQueue.Active = false;

            foreach (var p in producers)
            {
                p.Complete = true;
            }

            foreach (var c in consumers)
            {
                c.Complete = true;
            }

            for (int i = 0; i < (Producer.ThreadsRunning + Consumer.ThreadsRunning); i++)
            {
                lock (workQueue)
                {
                    Monitor.Pulse(workQueue);
                    Thread.Sleep(100);
                }
            }

            while ((Producer.ThreadsRunning > 0) || (Consumer.ThreadsRunning > 0))
            {
                ;
            }
            lblProcess.Text = "All producers and consumers shut down";
        }