예제 #1
0
        private void ThreadProc()
        {
            // Create a random transaction

            Random r = new Random();

            Customer randomCust = null;

            try
            {
                while (true)
                {
                    decimal randomTransAmt = Convert.ToDecimal(r.Next(1, Convert.ToInt32(_bank.MaxTransAmt)));
                    int     rndTransType   = r.Next(2);

                    TransactionType randomTransType = (TransactionType)rndTransType;

                    // Acquire next available customer
                    int i = r.Next(1, _bank.Customers.Count);
                    randomCust = _bank.Customers[i];

                    while (randomCust.IsAvailable == false)
                    {
                        Thread.Sleep(1);
                        randomCust = _bank.Customers[r.Next(1, _bank.Customers.Count)];
                    }

                    Transaction transaction = new Transaction(randomCust, randomTransType, randomTransAmt);

                    _bank.BankQueue.Enqueue(transaction);
                    if (quitEvent.WaitOne(50, true) == true)
                    {
                        listBoxHelper.AddString("Master: ThreadProc requested to stop.");
                        break;
                    }
                }
            }
            catch (ThreadInterruptedException tie)
            {
                listBoxHelper.AddString("Thread Interrupted Exception: \n" + tie.Message);
            }
            catch (ThreadAbortException tae)
            {
                listBoxHelper.AddString("Thread Abort Exception: \n" + tae.Message);
            }
        }
예제 #2
0
        private void ThreadProc()
        {
            while (true)
            {
                try
                {
                    int signal = WaitHandle.WaitAny(new WaitHandle[] { this._quitEvent, this._workToDoEvent }, 50, false);

                    if (signal == 0)
                    {
                        _listBoxHelper.AddString(string.Format("Teller Worker: {0} is stopping.", this.ID));
                        break;
                    }
                    else if (signal == 1)
                    {
                        bool        continueSimulation;
                        Transaction transaction = _bank.BankQueue.Dequeue();

                        if (transaction != null)
                        {
                            lock (transaction.Customer)
                            {
                                transaction.Customer.IsAvailable = false;
                                continueSimulation = ProcessTransaction(transaction);
                                transaction.Customer.IsAvailable = true;
                            }

                            if (continueSimulation == false)
                            {
                                _quitEvent.Set();
                            }
                        }
                    }
                }
                catch (ThreadInterruptedException tie)
                {
                    _listBoxHelper.AddString("Thread Interrupted Exception: \n" + tie.Message);
                }
                catch (ThreadAbortException tae)
                {
                    _listBoxHelper.AddString("Thread Abort Exception: \n" + tae.Message);
                }
                catch (Exception ex)
                {
                    _listBoxHelper.AddString("Exception: \n" + ex.Message);
                }
            }
        }