3 ms·
Async void methods are for asynchronous event handlers. [1] That's an unfortunate consequence of the way event handlers work in C# (event handlers that return
by JonathonW 11y ago
Async void methods are for asynchronous event handlers. [1] That's an unfortunate consequence of the way event handlers work in C# (event handlers that return a type don't work well in the language).
Any other use of async void is incorrect; if you see an async void method in any other context, replace it with async Task. (Would be nice if calling an async void method directly threw a compiler warning to make this completely clear.)
[1] https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=25...
- pionar 11y ago"Would be nice if calling an async void method directly threw a compiler warning to make this completely clear." With VS 2015 and Roslyn, you can easily achieve this. In fact, there's already sample code to do this in the roslyn samples project[0]. [0]https://github.com/dotnet/roslyn/tree/master/src/Samples/CSharp/AsyncPackage https://github.com/dotnet/roslyn/tree/master/src/Samples/CSh...
- JonathonW 11y agoThe ability to write my own analyzers shouldn't be a substitute for a well-curated set of code analysis checks out of the box, though. I want to spend my time building my product, not building the tooling so I can build my product. (Note that I haven't had the opportunity to play with VS2015 yet; for all I know, these checks from the Roslyn samples might already be provided with the IDE.)
- Locke1689 11y agoAnalyzers are available as NuGet packages, so it should be a trivial process to install one of these in your project. We're definitely going to start shipping some high-value analyzers in the box (probably including the async-void one).