Skip to content

void only for event handlers avoid

Context: async void should be used only for event handlers. For all other async methods, return Task or Task<T>.

using System;
using System.Threading.Tasks;
using System.Windows.Forms; // WinForms example
public partial class MyForm : Form
{
// Acceptable: event handler
private async void Button_Click(object sender, EventArgs e)
{
await LoadDataAsync();
}
private async Task LoadDataAsync()
{
await Task.Delay(100);
}
}
  • Exceptions cannot be caught by the caller; they crash the process.
  • Hard to test (no task to await).
  • Caller cannot know when the method completes.

UI event handlers: WPF, WinForms, and Blazor allow async void for events because the framework handles the exception.

Example: Async void pitfalls