Calling delegates later aka. delegate queue

1.8k views Asked by At

so I'm pretty new to c# and I've came around this problem: I have some methods that invoke different delegates (with potentially different argument types). However these delegates shouldn't be called right away. If the main Thread is running a delegate, they should run afterwards, kinda like queuing the delegate and running it later.
Now I could probably use DynamicInvoke but I don't know if it will slow the queue down too much, besides I know what the delegate type is and what kind of parameter it should look for, so its not really runtime dependent. Please help me if you can, I really need an answer.

Thanks everybody (who responds)

2

There are 2 answers

3
Tigran On

One of the ways is doing that could be use of Tasks (beginning from .NET 4.0)

Can have a look on

Asynchronous methods, C# iterators, and Tasks

how to create scheduling tasks, like, seems, in your case.

Hope this helps.

0
Douglas On

Would it work if, instead of adding your delegates directly, you wrapped them into new Action delegates which simply invoke them, passing all their parameters? For example:

List<Action> pending = new List<Action>();
pending.Add(() => MethodThatTakesNoParameters());
pending.Add(() => MethodThatTakesOneParameter(param));
pending.Add(() => MethodThatTakesThreeParameters(param1, param2, param3));