An async
method with a
void
return type does not follow the task asynchronous programming
(TAP) model since the return type should be Task
or Task<TResult>
Doing so prevents control over the asynchronous
execution, such as:
- waiting for the execution to complete
- catching any exception that might occur during execution
- testing execution behavior
Exceptions
- Methods with the
EventHandler
delegate signature. Using void
for EventHandler
is compliant with the TAP model.
public async void button1_Click(object sender, EventArgs e)
{
await DoSomethingAsync();
}
- Methods name matching
On[A-Z]\w*
pattern. Some frameworks may not use the same EventHandler
method signature
public async void OnClick(EventContext data)
{
await DoSomethingAsync();
}