.NET Parallel Programming

All about Async/Await, System.Threading.Tasks, System.Collections.Concurrent, System.Linq, and more…

Are you using TPL Dataflow? We’d love to know!

Are you using the new System.Threading.Tasks.Dataflow.dll library, either from its CTPs or from the .NET 4.5 Developer Preview or Beta?  We'd love to hear about it, and if you have time, what your experiences have been (good or bad).  What kind of solution are you building, and how are you using TPL Dataflow in it?  Has the ...

When “ExecuteSynchronously” doesn’t execute synchronously

When creating a task continuation with ContinueWith, developers have the opportunity to provide a TaskContinuationOptions enum value, which could include the TaskContinuationOptions.ExecuteSynchronously flag.  ExecuteSynchronously is a request for an optimization to run the continuation task on the same thread that completed the ...

Building a custom GetOrAdd method for ConcurrentDictionary

I was recently asked by a developer about getting some additional information out of ConcurrentDictionary<TKey,TValue>’s GetOrAdd method.  As a reminder, GetOrAdd either returns the value for a key currently in the dictionary, or if that key doesn’t have a value, it adds a value for the key as dictated by either a TValue...

Know Thine Implicit Allocations

For .NET 4.5, we’ve invested quite a bit of effort into performance, and in particular for the Task Parallel Library (Joe Hoag wrote a good paper covering some of these improvements).  We focused such effort on TPL because it is a core component used in async programming and at a foundational level for many libraries and ...

Await, SynchronizationContext, and Console Apps: Part 2

Yesterday, I blogged about how you can implement a custom SynchronizationContext in order to pump the continuations used by async methods so that they may be processed on a single, dedicated thread.  I also highlighted that this is basically what UI frameworks like Windows Forms and Windows Presentation Foundation do with their message ...

Implementing a SynchronizationContext.SendAsync method

I recently saw two unrelated questions, the answers to which combine to form a potentially useful code snippet.The first question was about SynchronizationContext.  SynchronizationContext provides a Post method, which asynchronously schedules the supplied delegate and object state to be executed according to the SynchronizationContext&...

FAQ on Task.Start

Recently I’ve heard a number of folks asking about Task.Start, when and when not to use it, how it behaves,and so forth.  I thought I’d answer some of those questions here in an attempt to clarify and put to rest any misconceptions about what it is and what it does.1. Question: When can I use Task.Start?The Start instance ...

PLINQ Queries That Run in Parallel in .NET 4.5

One interesting thing to know about PLINQ is that not all queries are guaranteed to execute in parallel (See PLINQ Queries That Run Sequentially for reference). You can think of the AsParallel method as a hint to run in parallel for query shapes that it believes will be faster. By default, PLINQ prefers to use a simple sequential algorithm ...

Crafting a Task.TimeoutAfter Method

Imagine that you have a Task handed to you by a third party, and that you would like to force this Task to complete within a specified time period. However, you cannot alter the “natural” completion path and completion state of the Task, as that may cause problems with other consumers of the Task. So you need a way to obtain a copy or “...