C#asynchronous programming
async
or Task
? void
method can return async
or Task
. This choice is essential because it determines the behavior of the method and its way of interacting with the call code. void
Task
. This allows the caller to wait for the task and continue to perform after it is completed. Back to Task
can also use abnormal processing and asynchronous mode, such as Task
, which can monitor the progress of multiple tasks. async-foreach
void
to async
is when you clearly need void
return type, for example, in the event processing program or UI callback. In these cases, the caller should not be able to wait for this method, and and void
keywords are not necessary. async
await
Avoid top -level Method
async void
async void
void
In this case, using
public static async void AsyncMethod2(int num) { await Task.Factory.StartNew(() => Thread.Sleep(num)); }
The abnormalities in the method will not be processed and may cause abnormal application behavior. async void
The above is the detailed content of Async/Await in C#: When to Return Task vs. Void?. For more information, please follow other related articles on the PHP Chinese website!