예제 #1
0
        public void FillHopper(Hopper hopper, int amount)
        {
            if (hopper.Content <= (2000 - amount))
            {
                int iterations = amount / 10;

                for (int i = 0; i < iterations; i++)
                {
                    hopper.Content += 10;
                    Spin.Wait(1);
                }
            }
        }
예제 #2
0
        public void Dispensing(bool isDispensing,
                               Hopper hopperFilling, Hopper hopperFlavor, Hopper hopperTopping,
                               ConcurrentBag <Crust> crusts, AutoResetEvent stopLucy, AutoResetEvent waitLucy)
        {
            while (isWorking)
            {
                stopLucy.WaitOne();
                isDispensing = true;



                while (!this.EnoughForDispensing(hopperFilling, 250))
                {
                    Spin.Wait(1);
                }
                this.AddFilling(crusts.ElementAt(Crust.numberOfPies - 1), hopperFilling);
                Console.WriteLine("Filling was added successfully.");


                while (!this.EnoughForDispensing(hopperFlavor, 10))
                {
                    Spin.Wait(1);
                }
                this.AddFlavour(crusts.ElementAt(Crust.numberOfPies - 1), hopperFlavor);

                Console.WriteLine("Flavor was added successfully.");


                while (!this.EnoughForDispensing(hopperTopping, 100))
                {
                    Spin.Wait(1);
                }
                this.AddTopping(crusts.ElementAt(Crust.numberOfPies - 1), hopperTopping);

                Console.WriteLine("Topping was added successfully.");

                isDispensing = false;
                waitLucy.Set();
            }
        }
예제 #3
0
        public static void Belt(bool isDispensing,
                                ConcurrentBag <Crust> crusts, AutoResetEvent stopLucy, AutoResetEvent waitLucy)
        {
            var stopwatch = new Stopwatch();


            while (isWorking)
            {
                stopwatch.Start();

                Crust newCrust = new Crust();
                crusts.Add(newCrust);

                stopLucy.Set();

                Spin.Wait(50);


                if (isDispensing)
                {
                    waitLucy.WaitOne();
                }

                stopwatch.Stop();

                Console.WriteLine("Pie was created successfully!");
                Console.WriteLine("Time taken for the pie to be created was {0}." + Environment.NewLine, stopwatch.ElapsedMilliseconds);
                stopwatch.Reset();

                if (stopFactory)
                {
                    isWorking = false;
                    isStopped = true;
                }
            }
        }
예제 #4
0
 public void Spinning()
 {
     Spin.Wait(10);
 }
예제 #5
0
        static void Main(string[] args)
        {
            Hopper hopperFilling = new Hopper();
            Hopper hopperFlavor  = new Hopper();
            Hopper hopperTopping = new Hopper();

            Joe  joe  = new Joe();
            Lucy lucy = new Lucy();

            AutoResetEvent stopLucy = new AutoResetEvent(false);
            AutoResetEvent waitLucy = new AutoResetEvent(false);

            ConcurrentBag <Crust> crusts = new ConcurrentBag <Crust>();

            bool isDispensing          = false;
            bool stopFactory           = false;
            bool isStoppedSuccessfully = false;

            string startingTheFactory = "START WORKING";

            Console.WriteLine("Press \"ENTER\" to start the factory and press \"ESCAPE\" to stop it.");

            while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter))
            {
                ;
            }

            for (int i = 0; i < 5; i++)
            {
                Console.SetCursorPosition(0, 1);
                startingTheFactory += '.';
                Console.WriteLine(startingTheFactory);
                Spin.Wait(450);
            }
            Console.WriteLine();

            void ConveyorBelt()
            {
                Conveyor.Belt(isDispensing,
                              crusts, stopLucy, waitLucy);
            }

            void WorkingLucy()
            {
                lucy.Dispensing(isDispensing,
                                hopperFilling, hopperFlavor, hopperTopping, crusts, stopLucy, waitLucy);
            }

            void WorkingJoe()
            {
                joe.FillHoppers(hopperFilling, hopperFlavor, hopperTopping);
            }

            Thread Belt = new Thread(new ThreadStart(ConveyorBelt));

            Thread RobotLucy = new Thread(new ThreadStart(WorkingLucy));

            Thread RobotJoe = new Thread(new ThreadStart(WorkingJoe));


            RobotJoe.Start();
            Belt.Start();
            RobotLucy.Start();

            while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
            {
                ;
            }
            Joe.isWorking        = false;
            Conveyor.stopFactory = true;
            do
            {
                if (Conveyor.isStopped)
                {
                    isStoppedSuccessfully = true;
                    Console.WriteLine("Pie factory stopped working successfully!");
                }
            } while (!isStoppedSuccessfully);
        }