In my .NET Framework 4.5
Windows Forms project I have an async event handler. In the event handler I open a OpenFileDialog
with ShowDialog()
. Then I want to do something async with the selected file.
But I have some weird behavior: After closing the dialog (with Cancel
or OK
button) I have got a delay of 9 seconds until the ShowDialog
returned with its result. While this time the application is freezed.
Here my code:
private async void buttonBrowse_Click(object sender, EventArgs e)
{
DialogResult result = this.openFileDialog.ShowDialog(this);
if (result != DialogResult.OK) // <- delayed more than 9 seconds after user closes dialog
return;
await this.LoadFileAsync(this.openFileDialog.FileName);
}
After I remove the keyword async
then the code behaves as expected:
private void buttonBrowse_Click(object sender, EventArgs e)
{
DialogResult result = this.openFileDialog.ShowDialog(this);
if (result != DialogResult.OK) // -> no delay here
return;
this.LoadFileAsync(this.openFileDialog.FileName); // works, but compiler warning, because missing (await-keyword)
}
Can somebody please explain this behavior? Thanks.
Yes, I know the workaround: I could use the event handler of the dialog FileOk
and move my code to this event handler. But I am curious about the documented behavior.