Friends Don't Let Friends Async Void

When writing asynchronous code using the Microsoft .NET Framework 4.5, it's tempting and easy to declare methods such as async void. Don't do this. You should only use async void on top-level event handlers. Instead, declare your methods as async Task. The async void methods wind up being fire-and-forget methods. That's the case even when awaited, because there's no task to actually await. Also, exceptions won't be properly propagated to your try/catch block.

Brian Peek is a senior program manager at Microsoft. Previously a Microsoft MVP in the C# discipline, Peek specializes in software development using a variety of Microsoft technologies and platforms and is also well-versed in hardware projects, graphics and game development.

Posted by Brian Peek on 04/15/2015

Keep Up-to-Date with Visual Studio Live!

Email address*Country*