This is a mirror of official site: http://jasper-net.blogspot.com/

Simple golden rules for async / await

| Thursday, February 14, 2013
The async / await features in C# are incredibly easy to use but there’s a number of pitfalls than can be trap the unwary. The following are my golden rules when building any app (usually Windows 8 but now also Windows Phone 8) using the async / await features.

Always await your async operations, sometimes it’s easy to not await an async operation if you don’t need the result. By not awaiting the operation it becomes “fire and forget”, however this also means that any exceptions in the operation are swallowed and the operation could fail silently. I make sure all async operations are properly awaited so I can keep track of any errors in the system. If you need to make the operation “fire and forget” there are patterns you can use (I’ll go into these in a later post) to combine operations so you’re not holding other activity up.

Be careful around async void, there’s already advice on the internet to avoid async void methods as much as possible, but they have a habit of creeping in almost everywhere. In Windows 8 an exception that leaves an async void method will crash the process while also avoiding the Unhandled Exception handler (this does appear different in Windows Phone 8). Unfortunately you can’t easily get away from then, given that a lot of your async activity is driven from event handlers which will be async void. My golden rule is to have a strategy in error handling to make sure all errors are handled in every async void. I discuss one possible strategy in my Tech Ed talk on Windows 8 app development.

Be very careful around async lambdas, it can be very easy to use the async / await keywords inside lambdas, what is not easily apparent is the signature of the lambda. The same async lambda can be either an Action or a Func depending where you pass it, if it’s Action then you’re suddenly writing yourself an async void and the above advice applies, it also can’t be awaited with random effects. Phil Haack easily demonstrates what can happen if you get it wrong.

QR: Inline image 1

Posted via email from Jasper-net

0 comments: