- Dev Blogs
- .NET Parallel Programming
.NET Parallel Programming
All about Async/Await, System.Threading.Tasks, System.Collections.Concurrent, System.Linq, and more…
Latest posts
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 provided by the caller or by executing a Func<TKey,TValue> provided by the caller. It then returns the new value. However, it doesn’t tell the caller which happened; all the caller knows is that it’s handed back the value (existing or new) that...
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 applications. Small tweaks to the performance characteristics of TPL can thusly have a noticeable impact on the performance of these higher-level systems.In particular, one of the areas we focused a lot of attention on is memory utilization: how many objects are we allocating, an...
Await, SynchronizationContext, and Console Apps: Part 3
In Part 1 and Part 2 of this short series, I demonstrated how you can build a SynchronizationContext and use it run an async method such that all of the continuations in that method will run on serialized on the current thread. This can be helpful when executing async methods in a console app, or in a unit test framework that doesn’t directly support async methods. However, the support I showed thus far targets async methods that return Task… what about async methods that return void? C# and Visual Basic support two flavors of async methods: ones that return tasks (either Task or Task<...
Advanced APM Consumption in Async Methods
I’ve previously blogged about how to expose existing Asynchronous Programming Model (APM) implementations as Task-based methods. This can be done manually using a TaskCompletionSource<TResult>, or it can be done using the built-in wrapper provided in TPL via the Task.Factory.FromAsync method. By creating a Task-based wrapper for such BeginXx/EndXx method pairs, APM implementations become consumable in async methods in C# and Visual Basic: since Tasks are awaitable, any operation exposed as a Task is then inherently awaitable.What I haven’t before discussed is how you can consume an A...
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 pumps.Now that we understand the mechanics of how these things work, it’s worth pointing out that we can achieve the same basic semantics without writing our own custom SynchronizationContext. Instead, we can use one that already exists in the .NET Framework: Dispa...
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’s whims. However, Post returns void, so there’s no built-in way to know when the operation has completed. SynchronizationContext also provides a Send method, which won’t return until the delegate has been executed; that means that while you do have...
Await, SynchronizationContext, and Console Apps
When I discuss the new async language features of C# and Visual Basic, one of the attributes I ascribe to the await keyword is that it “tries to bring you back to where you were.” For example, if you use await on the UI thread of your WPF application, the code that comes after the await completes should run back on that same UI thread. There are several mechanisms that are used by the async/await infrastructure under the covers to make this marshaling work: SynchronizationContext and TaskScheduler. While the transformation is much more complicated than what I’m about to show, logically you can t...
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 method may be used if and only if the Task is in the Created state (i.e. Task.Status returns TaskStatus.Created). And the only way a Task can be in the Created state is if the Task were instantiated using one of Task’s public constructors, e.g. "var t = new Task(some...
Awaiting Socket Operations
The System.Net.Sockets.Socket class in .NET exposes multiple sets of asynchronous methods that perform the same basic operations but that are exposed with different patterns.The first set follows the APM pattern, where for a synchronous method like Receive, the BeginReceive and EndReceive methods are exposed. If you want to be able to “await” such asynchronous operations in an async method, the easiest way to do so is to create a Task-based wrapper. That can be done using Task.Factory.FromAsync, or it can be done more manually by using a TaskCompletionSource<TResult>, e.g. public sta...