Ejemplo n.º 1
0
        /// <summary>
        /// Select whether to take from AP or CP
        /// If both pools are empty wait method is called
        /// </summary>
        public void RemoveCustomer()
        {
            selectPool = rnd.Next(1, 3);
            Thread.Sleep(rnd.Next(100, 200));


            if (selectPool == 1 && cpPool.Empty == false)//take from cp
            {
                Customer temp = cpPool.RemoveFromPool();
                exitQueue.Enqueue(temp);
                ++currentInExitQueue;
                ++exitAP;

                label1.Invoke(new Action(delegate() { label1.Text = exitCP.ToString(); }));

                Thread.Sleep(rnd.Next(200, 500));
            }
            else if (selectPool == 2 && adPool.Empty == false)//take from ap
            {
                Customer temp = adPool.RemoveFromPool();
                exitQueue.Enqueue(temp);
                ++currentInExitQueue;
                ++exitCP;

                label2.Invoke(new Action(delegate() { label2.Text = exitAP.ToString(); }));

                Thread.Sleep(rnd.Next(200, 500));
            }
            Wait();
        }
        /// <summary>
        /// Adds people from waiting queue
        /// It can also take people from the adventure pool
        /// If the pool are full it calls wait method
        /// </summary>
        public void FillPool()
        {
            while (!filled)
            {
                if (wQueueCP.Empty == true)
                {
                    break;
                }

                selectCustomerFrom = rnd.Next(1, 3);

                if (selectCustomerFrom == 1)//common pool
                {
                    Monitor.Enter(myLock);
                    p1.Invoke(new Action(delegate() { p1.BackColor = Color.Green; }));
                    while (customerQueue.Count >= maxCapacity)
                    {
                        Monitor.Wait(myLock);
                        p1.Invoke(new Action(delegate() { p1.BackColor = Color.DarkRed; }));
                        //poolfärg röd
                    }

                    Monitor.PulseAll(myLock);

                    Customer temp = wQueueCP.RemoveFromQueue();
                    customerQueue.Enqueue(temp);
                    ++currentNumberOfVisitors;

                    if (currentNumberOfVisitors >= maxCapacity)
                    {
                        filled = true;
                    }
                    empty = false;
                    label1.Invoke(new Action(delegate() { label1.Text = currentNumberOfVisitors.ToString(); }));


                    Monitor.PulseAll(myLock);
                    Monitor.Exit(myLock);

                    Thread.Sleep(rnd.Next(150, 500));
                }

                else if (selectCustomerFrom == 2)//from adventure pool
                {
                    Monitor.Enter(myLock);
                    label2.Invoke(new Action(delegate() { label2.Text = "Ready to move from CommonPool to AdventurePool "; }));
                    while (customerQueue.Count >= maxCapacity)
                    {
                        Monitor.Wait(myLock);
                    }

                    Monitor.PulseAll(myLock);

                    Customer temp = apClass.RemoveFromPool();
                    customerQueue.Enqueue(temp);
                    ++currentNumberOfVisitors;

                    if (currentNumberOfVisitors >= maxCapacity)
                    {
                        filled = true;
                    }
                    empty = false;

                    label2.Invoke(new Action(delegate() { label2.Text = "Moved one "; }));
                    label1.Invoke(new Action(delegate() { label1.Text = currentNumberOfVisitors.ToString(); }));


                    Thread.Sleep(rnd.Next(150, 500));
                    Monitor.PulseAll(myLock);
                    Monitor.Exit(myLock);

                    label2.Invoke(new Action(delegate() { label2.Text = "Waiting to move from Common Pool "; }));
                }
            }
            Wait();
            //if (currentNumberOfVisitors >= maxCapacity) { filled = true; }
            //else { filled = false; }
        }