Sergio Solorzano
Sergio Solorzano

Reputation: 665

Queued function not executing with dequeue

Dequeue does not call the queued function, console shows there is 1 action queued.

This code:

private static Queue<Action> changeMaterialTask = new Queue<Action>();

static void Main(string[] args)
{
    AddAction(() => Test());
    while (changeMaterialTask.Count > 0)
    {
        Console.WriteLine("About to deque");
        changeMaterialTask.Dequeue();
        Console.WriteLine("I've dequeued");
    }
}

public static void AddAction(Action task)
{
    changeMaterialTask.Enqueue(task);
}

public static void Test()
{
    Console.WriteLine("Worked");
}

print on console "Worked" when queued function executes at dequeue.

Upvotes: 1

Views: 244

Answers (1)

DavidG
DavidG

Reputation: 118987

The Queue<T>.Dequeue method returns the item you have dequeued from the queue, in this case an Action. You then need to do something with it, for example:

var action = changeMaterialTask.Dequeue();
action();

Upvotes: 5

Related Questions