Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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 ValueTaskfor 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!



This post first appeared on Shades Of Orange | Our Code Is Orange - Softwar, please read the originial post: here

Share the post

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

×

Subscribe to Shades Of Orange | Our Code Is Orange - Softwar

Get updates delivered right to your inbox!

Thank you for your subscription

×