Is there a secure way to not wait for a process in .NET?
async Task DoFoo()
{
// ...
GravarLog();
// ...
}
void GravarLog()
{
// ...
}
In the code above, my whole process will wait for the GravarLog()
method to finish, which for me is not necessary, and this method could be executed in background , without my having to wait for it to run the rest of my code. (logging was just an example I used to contextualize)
An alternative, however very volatile, would be async void
as a fire and forget ":
async Task DoFoo()
{
// ...
GravarLog();
// ...
}
async void GravarLog()
{
// ...
}
There are many articles and reviews saying to avoid at all costs to use async void
, because exception control is different from the .NET standard:
- haacked.com: Avoid async void methods
- MSDN: Async / Await - Best Practices in Asynchronous Programming
- SOpt: When Do not Return Task on Asynchronous Methods?
How do not you have to "wait" for a method to finish safely?