BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Asynchronous Gotchas in C#

| by Jonathan Allen on Apr 17, 2013. Estimated reading time: 1 minute |

Sometimes the best way to understand pitfalls in one language is to see how another language prevents them. Tomas Petricek, author of Real-World Functional Programming, discusses six common mistakes found in asynchronous C# code and demonstrates how F# makes them less likely to occur.

While we encourage you read his entire article, titled Async in C# and F#: Asynchronous gotchas in C#, here is a brief synopsis of Tomas covers.

Async does not run asynchronously: Only code that appears after the first await statement is run asynchronously.

Ignoring results: Forgetting to await on the task returned by a function can result in out-of-order execution.

Async void methods: Asynchronous functions that return “async void” instead of “async Task” cannot be awaited, effectively causing the same problem as ignoring results.

Async void lambda functions: This occurs when a function accepts an Action delegate but not a Func<…, Task> delegate. Again, the async function is not going to be awaited on.

Nesting of tasks: In the statement “await Task.Factory.StartNew(async () => { await Task.Delay(1000); });” the first and second await statements are completely unrelated. This means the first await will complete before second await, and the associated 1000 ms delay, are honored.

Not running asynchronously: By using Task.Wait(), the entire call stack can be forced into a synchronous mode.

For those of you unfamiliar with F#, you may be surprised to learn that async workflows in F# are not based on the Task and Task<T> types. Instead, F# uses its own type known as Async<T>.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Maybe a typo? by 姚 琪琳

In the last paragraph but tow, "are honored" should be "are ignored" according Petricek's blog

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT