Пример #1
0
        public void Work(TimeSpan workDay, StockController controller, LogTradesQueue bonusQueue)
        {
            DateTime start = DateTime.Now;

            while (DateTime.Now - start < workDay)
            {
                string msg = ServeCustomer(controller, bonusQueue);
                if (msg != null)
                {
                    Console.WriteLine($"{Name}: {msg}");
                }
            }
        }
        public static void Run()
        {
            StockController controller  = new StockController();
            TimeSpan        workDay     = new TimeSpan(0, 0, 0, 0, 500);
            StaffRecords    staffLogs   = new StaffRecords();            //represents data store
            LogTradesQueue  tradesQueue = new LogTradesQueue(staffLogs); //Queue

            SalesPerson[] staff =
            {
                new SalesPerson("Jack"),
                new SalesPerson("Anna"),
                new SalesPerson("Kim"),
                new SalesPerson("Mike")
            };
            List <Task> salesTasks = new List <Task>();

            foreach (SalesPerson person in staff)
            {
                salesTasks.Add(
                    Task.Run(() => person.Work(workDay, controller, tradesQueue)));
            }

            Task[] loggingTasks =
            {
                Task.Run(() => tradesQueue.MonitorAndLogTrades()),
                Task.Run(() => tradesQueue.MonitorAndLogTrades())
            };

            //Here SalesPerson threads ENQUEUE items, Logging threads DEQUEUE items.they are completely different threads for Adding & Removing.
            //So, ConcurrentBag<T> is not good fit in this project.but demo is in "LogTradesQueue"

            Task.WaitAll(salesTasks.ToArray());
            tradesQueue.SetNoMoreTrades();
            Task.WaitAll(loggingTasks);

            controller.DisplayStock();
            staffLogs.DisplayCommissions(staff);
        }