Пример #1
0
        public async void StartCookingProcess(CancellationToken token)
        {
            while (true)
            {
                Task[] tasks = new Task[CountOfMenu * Enum.GetNames(typeof(DishesEnum)).Length];
                for (int i = 0; i < tasks.Length; i++)
                {
                    int Id = i;
                    if (token.IsCancellationRequested)
                    {
                        LogSys.WriteToLogFile("Работа кухни остановлена!");
                        return;
                    }

                    tasks[Id] = Task.Run(() =>
                    {
                        Thread.Sleep(1000 + Id * 50);
                        AddDish?.Invoke(this, new AddDishEventArgs
                        {
                            DishToAdd = DictionOfTypes[(DishesEnum)(Id % 3)].Invoke(Id + "_" + (int)TotalDate.DayOfWeek)
                        });
                    });
                }
                Task.WaitAll(tasks);
                TotalDate.AddDays(1);
            }
        }
Пример #2
0
 public void AddDishToList(object sender, AddDishEventArgs args)
 {
     lock (_dishes)
     {
         _dishes.Enqueue(args.DishToAdd);
         LogSys.WriteToLogFile(args.DishToAdd.Name + " поступило в продажу с кухни!");
     }
 }
Пример #3
0
        public async void StartSellingProcess(CancellationToken token)
        {
            while (true)
            {
                Task[] tasks = new Task[CountOfClients];
                for (int i = 0; i < tasks.Length; i++)
                {
                    int Id = i;
                    if (token.IsCancellationRequested)
                    {
                        LogSys.WriteToLogFile("Работа ресторана остановлена!");
                        return;
                    }

                    tasks[Id] = Task.Run(() =>
                    {
                        Thread.Sleep(1000 + Id * 40);
                        lock (_dishes)
                        {
                            try
                            {
                                Dish dishToSell = _dishes.Dequeue();
                                if ((DishesKitchen.TotalDate - dishToSell.Cooked).Days >= dishToSell.ShelfLife)
                                {
                                    LogSys.WriteToLogFile(dishToSell.Name + " просрочен и будет выброшен!");
                                }
                                else
                                {
                                    LogSys.WriteToLogFile(dishToSell.Name + $" продан посетителю №{Id}!");
                                }
                            }
                            catch (Exception ex)
                            {
                                LogSys.WriteToLogFile("Нету блюд к продаже!");
                            }
                        }
                    });
                }
                Task.WaitAll(tasks);
            }
        }