Exemplo n.º 1
0
        /// <summary>
        /// Farms some chickens.
        /// </summary>
        public void FarmSomeChickens()
        {
            CancellationToken token = cancelSource.Token;
            timeFarmStarted = DateTime.UtcNow;
            UpdatePrice();
            var multiCellBuffer = new MultiCellBuffer(token);

            while (!token.IsCancellationRequested)
            {
                try
                {
                    string newOrderEncoded;
                    if (multiCellBuffer.GetOneCell(out newOrderEncoded))
                    {
                        var newOrder = OrderClass.Decode(newOrderEncoded);
                        averageNumberOfChickens = (newOrder.Amount + averageNumberOfChickens) / 2.0;
                        timeLastOrdered = DateTime.UtcNow;
                        var orderProcessing = new OrderProcessing(newOrder, GetPrice());
                        var OrderProcessingThread = new Thread(orderProcessing.ProcessOrder) { Name = "OrderProcessing" + newOrder.SenderId };
                        OrderProcessingThread.Start();
                    }
                    UpdatePrice();
                }
                catch (OperationCanceledException e)
                {
                    Debug.WriteLine("A cancellation for {0} is requested.", Thread.CurrentThread.Name);
                }
            }
            Console.WriteLine("Total Time: {0} ms", (DateTime.UtcNow - timeFarmStarted).Milliseconds);
        }
Exemplo n.º 2
0
        /*
         * EVENT HANDLER (this will be used to actually process our purchase order.)
         * It receives the orders from the MultiCellBuffer (MCB). For each order, a new thread is started
         * (resulting in multiple threads for processing multiple orders) from OrderProcessing class in
         * order to process the purchase order based on the current price.
         */
        public void processPO()
        {
            OrderClass purchaseOrder = Program.MCB.getOneCell();    // retrieves our PO from our MCB

            // creates a new thread for each new order from the OrderProcessing class in order to process our purchase order.
            Thread processPO_Thread = new Thread(() => OrderProcessing.orderProcess(purchaseOrder, get_ticketPrice()));


            processPO_Thread.Start();
        }
Exemplo n.º 3
0
        //Printing Order Confirmation
        public void ProcessOrder(OrderClass o, double currentPrice)
        {
            if (o.PublisherId == Thread.CurrentThread.Name)
            {
                //need to add logic to this for processing order
                Console.WriteLine("Order for bookstore {0} received", Thread.CurrentThread.Name);
                OrderProcessing orderProcessing = new OrderProcessing(o);
                o.Current_Price_order = currentPrice;
                Thread p_thread = new Thread(new ThreadStart(orderProcessing.processOrder));
                processingThreads.Add(p_thread);
                p_thread.Name = "p_thread " + Thread.CurrentThread.Name;
                p_thread.Start();
            }

            else
            {
                Console.WriteLine("This order is not for Publisher {0} " + Thread.CurrentThread.Name);
            }
        }