.NET Developer Days 2017: Async/Await and the Task Parallel Library – by Daniel Marbach

by Oliver 28. November 2017 10:58

Disclaimer: This is just a bunch of short notes I took during a conference session held by Daniel Marbach at the .NET Developer Days. To dive deeper into the topic, please head over to the following GitHub repo, run the examples, and examine the source code: https://github.com/danielmarbach/Await.HeadExplosion/tree/master/Presentation. My take aways: use Task.Run() for compute-bound operations, avoid the low-level method Task.Factory.StartNew() in the async world, use await Task.Delay() instead of Thread.Sleep() since .NET Framework 4.6, there's a cached static instance Task.CompletedTask that can be used whereever you want to return a completed task – no allocation needed opt out of context capturing by calling .ConfigureAwait(false) – when the previous execution context is not needed for the continuation of the async task concurrency != parallelism: a lot of tasks can be handled by a single thread-pool thread concurrently – only one thread will be used tasks can be split across many threads to achieve parallelism – when desired/necessary use SemaphoreSlim to limit parallelism, e.g. to use only 3 threads at the most use ValueTask<T> for high performance code – wrapper for situation where either a Task is supposed to be executed or its return value is already available, e.g. cached Happy asyncing!

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our three children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.