public ConsumerSplitter(BufferTray <Drink> mainTray, BufferTray <Drink> beerTray, BufferTray <Drink> sodaTray, MessageEvent processInfo)
 {
     MainTray    = mainTray;
     BeerTray    = beerTray;
     SodaTray    = sodaTray;
     ProcessInfo = processInfo;
 }
        private void ConsumerProcess(BufferTray <Drink> tray)
        {
            Consumer consumer = new Consumer(tray, PulledDrink);

            try
            {
                consumer.Work();
            }
            catch (Exception ex)
            {
                ProcessInfo?.Invoke(ex.Message);
            }
        }
        public void Work()
        {
            while (true)
            {
                if (Monitor.TryEnter(MainTray))
                {
                    if (MainTray.Position > 0)
                    {
                        Drink drink             = MainTray.Pull();
                        BufferTray <Drink> tray = GetTray(drink.Type);

                        if (tray.Position < tray.Length)
                        {
                            if (Monitor.TryEnter(tray))
                            {
                                tray.Push(drink);

                                ProcessInfo?.Invoke($"{drink.Type} was splitted onto {drink.Type} tray");

                                Monitor.Pulse(tray);
                                Monitor.Exit(tray);

                                Thread.Sleep(rng.Next(50, 250));
                            }
                        }
                        else
                        {
                            MainTray.Push(drink);
                        }
                    }

                    Monitor.Pulse(MainTray);
                    Monitor.Exit(MainTray);
                }
            }
        }
Exemplo n.º 4
0
 public Producer(BufferTray <Drink> mainTray, MessageEvent processInfo)
 {
     MainTray    = mainTray;
     ProcessInfo = processInfo;
 }
 public BottleVendingMachine()
 {
     MainTray = new BufferTray <Drink>(MAX_TRAY_ITEMS);
     BeerTray = new BufferTray <Drink>(MAX_TRAY_ITEMS);
     SodaTray = new BufferTray <Drink>(MAX_TRAY_ITEMS);
 }
 public Consumer(BufferTray <Drink> tray, PulledDrinkEvent pulledDrink)
 {
     Tray        = tray;
     PulledDrink = pulledDrink;
 }