Cycle Queue in C #

     public List<Transfer> Queue
     {
         get { return _queue; }
         set { _queue = value; }
     }
    TransferFromQueue()
    {
         // Do stuff
         // Remove transfered item from Queue
    }

My turn is a variable in which elements will be added and deleted all the time.

I am looking for a way to execute TransferFromQueue () whenever there are items in the list. Although it should never run more than one TransferFromQueue () value.

How can I loop this queue in turn when there are elements in the queue?

+4
source share
1 answer

Why don't you use it Queue<Transfer>? This seems to be exactly what you want.

private Queue<Transfer> _queue = new Queue<Transfer>();
public Queue<Transfer> Queue
{
    get { return _queue; }
    set { _queue = value; }
}

void TransferFromQueue()
{
    while(Queue.Count > 0)
    {
        Transfer current = Queue.Dequeue(); // removed
        // use Queue.Peek() if you want to look at it witout removing it
        // Do stuff
    }
}

on this topic:

Queue <T> vs List <T>

+8
source

All Articles