Hi,
I am trying to understand the microsoft documentation on how to handle reentrancy issues. One of the methods it recommends is to disable the button so the user can't start another async operation before the original one is finished. The code microsoft provides is below:
Code:
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
// This line is commented out to make the results clearer in the output.
//ResultsTextBox.Text = "";
// ***Disable the Start button until the downloads are complete.
StartButton.IsEnabled = false;
try
{
await AccessTheWebAsync();
}
catch (Exception)
{
ResultsTextBox.Text += "\r\nDownloads failed.";
}
// ***Enable the Start button in case you want to run the program again.
finally
{
StartButton.IsEnabled = true;
}
}
My question is why do we have to put StartButton.IsEnabled = true in a finally block ? Can't we put it right after the await statement ? I thought await prevented further code from executing in an async method unil the task was completed.