Example #1
0
        static void Main(string[] args)
        {
            ChickenFarm     chicken   = new ChickenFarm();
            MultiCellBuffer buffer    = new MultiCellBuffer();
            OrderProcessing processor = new OrderProcessing();

            //Names of threads that represent retailers
            string[] retailerNames = { "Bob's Chickens", "Chickens R Us", "Turkey Jerky",
                                       "JamJam inc",     "Enterprise Chickens" };
            Thread   farmer = new Thread(new ThreadStart(chicken.runFarmer));

            farmer.Start();
            Retailer chickenstore = new Retailer();

            //retailers subscribe to price cut event
            ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);
            //retailer threads subscribe to farmer thread termination event to stop retailer threads
            ChickenFarm.destroyThreads += new destroyThreadsEvent(chickenstore.destroyThreads);
            //retailer subscribes to order confirmation event
            OrderProcessing.OrderConfirmation += new OrderConfirmationEvent(chickenstore.confirmation);
            //start retailer threads and assign names
            Thread[] retailers = new Thread[5];
            for (int i = 0; i < 5; i++)
            {
                retailers[i]      = new Thread(new ThreadStart(chickenstore.runRetailer));
                retailers[i].Name = retailerNames[i];
                retailers[i].Start();
            }
            //release semaphore to allow threads access to the buffer
            buffer.releaseSemaphore();
            //hold console open after completion
            Console.Read();
        }
        static void Main(string[] args)
        {
            int noOfBufferCells = 3; // Buffer Capacity- Number of cells in Multicellbuffer
            int N = 5; //Number of Reatilers

            ChickenFarm chickenFarm = new ChickenFarm();
            Thread chickenFarm_thread = new Thread(new ThreadStart(chickenFarm.farmerFunc));

            CreditCardHelper creditCardHelper = new CreditCardHelper(N);
            MultiCellBuffer buffer = new MultiCellBuffer(noOfBufferCells);

            buffer.bufferSemaphore.Release(noOfBufferCells);
            OrderProcessing orderProcessing = new OrderProcessing(buffer, creditCardHelper);

            chickenFarm_thread.Name = "ChickenFarm";
            chickenFarm_thread.Start();
            Retailer retailer = null;
            for (int i = 0; i < N; i++)
            {
                retailer = new Retailer(i, buffer, creditCardHelper);
                chickenFarm.priceCut += new priceCutEvent(retailer.chickensOnSale);
            }
            buffer.newOrder += new newOrderEvent(orderProcessing.orderProcessing);
            orderProcessing.orderCompleted += new orderCompletedEvent(retailer.confirmationFunc);
            Console.ReadKey();
        }