I know that it is usually a bad idea to use fire-and-forget async void methods to start tasks, because there is no way to a waiting task, and it is difficult to handle exceptions that can be thrown inside such a method.
Should I avoid async void event handlers at all? For example,
private async void Form_Load(object sender, System.EventArgs e) { await Task.Delay(2000);
I can rewrite it like this:
Task onFormLoadTask = null; // track the task, can implement cancellation private void Form_Load(object sender, System.EventArgs e) { this.onFormLoadTask = OnFormLoadTaskAsync(sender, e); } private async Task OnFormLoadTaskAsync(object sender, System.EventArgs e) { await Task.Delay(2000); // do async work // ... }
What are the pitfalls for asynchronous event handlers besides the possible re-entry?
avo 16 Oct '13 at 23:21 2013-10-16 23:21
source share