I'm trying async await for the first time and running into a problem with using Progress. I call my async method using await and pass in my progress action. What I would like to happen is the progress would be displayed and when the method completes the message to say we're "Done!" is displayed. What happens is that the progress is displayed, "Done!" is displayed and then the last progress message, this is an example of the output: 1, 2,
Done! 3
It appears that the async has returned and the UI context executes before the final Progress has had a chance to run. I can overcome this by adding handlers but with rather follow the pattern correctly.
public async void button1_Click(object sender, EventArgs e)
{
await JustDoItAsync(new Progress<int>(ProgressUpdate));
textBox1.Text += "\r\nDone!\r\n";
}
public async Task JustDoItAsync(IProgress<int> progress)
{
for (int i = 0; i < 3; i++)
{
await Task.Delay(500);
progress.Report(i + 1);
}
}
private void ProgressUpdate(int step)
{
textBox1.Text += step + "\r\n";
}