I have an async delegate which I await in the async method:
async Task M1()
{
Debug.WriteLine("M1.A");
await Task.Delay(10);
Debug.WriteLine("M1.B");
}
async Task M2()
{
Debug.WriteLine("M2.A");
await Task.Delay(1);
Debug.WriteLine("M2.B");
}
delegate Task MyDel();
async void button_Click(object sender, RoutedEventArgs e)
{
MyDel del = null;
del += M1;
del += M2;
await del();
}
The output is:
M1.A
M2.A
M2.B
M1.B
That is, both invocation members go off simultaneously, not awaiting each other. I need them await each other so the output would be:
M1.A
M1.B
M2.A
M2.B
I tried this in place of await del():
foreach (MyDel member in del.GetInvocationList())
{
await member();
}
This works. However, I have lots of code places where this needs to be done. Delegates can have different number of parameters of various types but they all return Task.
How can I write an extension method which will let me run the code above by making calls like this?
del0.AwaitOneByOne(); // del0 is 'delegate Task Method()'
del1.AwaitOneByOne(paramInt1, paramStr2); // del1 is 'delegate Task Method(int, string)'
del2.AwaitOneByOne(paramBytes1); // del2 is 'delegate Task Method(byte[])'
delegatebecause it provides add/remove and signature check for free. So just as an idea it can be easier to take a look ateventand override add / remove to custom method which will chain tasks instead of paralleling them. Of course if you need to "raise event" outside its owner there won't be easy, so you may want to abstract that in some helper class.