Пример #1
0
        // This is our default method that will run when the thread starts from Program.cs
        public void farmerFunc()
        {
            // Will run until we reach 10 price cuts
            while (cutCount < 10)
            {
                // Iterates every 500 milliseconds
                Thread.Sleep(500);

                // Generate a random price using PricingModel() method
                int p = PricingModel();

                // Change the chicken price to our price generated from PricingModel()
                ChickenFarm.changePrice(p);
            }

            // Once the loop is complete and 10 price cuts have happened, we can terminate our threads
            Program.farmThreadActive = false;
        }
        static void Main(string[] args)
        {
            // Initialize our ChickenFarm
            ChickenFarm chicken = new ChickenFarm();

            // Initialize our Buffer
            buffer = new MultiCellBuffer();

            // Start our ChickenFarm thread
            Thread farmer = new Thread(new ThreadStart(chicken.farmerFunc));

            farmer.Start();

            // Initialize our Retailer
            Retailer chickenstore = new Retailer();

            // ChickenFarm communicates to the subscribed retailer when a price cut happens, then the retailer generates an order
            ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);

            // Retailer communicates to the ChickenFarm when an order is generated, then the ChickenFarm processes the order
            Retailer.orderGenerated += new orderGeneratedEvent(chicken.receiveOrder);

            // Once the respective retailer gets confirmation of order processing, the order is printed
            OrderProcessing.orderProcessed += new orderProcessedEvent(chickenstore.orderProcessed);

            // Initialize 5 new threads for our retailers
            retailers = new Thread[5];

            for (int i = 0; i < 5; i++)
            {
                // Make 5 copies of our retailer into our 5 Threads
                retailers[i] = new Thread(new ThreadStart(chickenstore.retailerFunc));

                // Give our threads a name (1 through 5 respectively)
                retailers[i].Name = (i + 1).ToString();

                // Then start our 5 retailer threads
                retailers[i].Start();
            }
        }