I need to call multiple async methods and inside of them, call another async method aswell. Let me demonstrate
private async void button1_Click(object sender, EventArgs e)
{
for(int i = 0; i< 100; i++)
{
await Method1();
}
}
public async Task Method1()
{
await Task.Delay(3*1000);
await Method2();
}
public async Task Method2()
{
await Task.Delay(10*1000);
}
My problem is, the for statement only activates the iterations after the wait on Method2 starts and what I want is to create the 100 Task all at once. Everything else will be done asynchronously.
I think you are confused as to what "await" means. "await" means "start processing this thing asynchronously, and while that is ticking away, go back to the windows message loop and keep on processing messages so that the UI keeps on re-drawing. When the asynchronous task is done, pick up the code where I awaited".
So when you await
in a loop like that, you are saying:
If that's not what you want then don't await the task. If you want to start a hundred tasks that run concurrently then start a hundred tasks and don't await any of them. Why is there any await in the click handler?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With