Stephen Toub - MSFT

Partner Software Engineer, .NET

Stephen Toub is a developer on the .NET team at Microsoft.

Post by this author

Building Async Coordination Primitives, Part 2: AsyncAutoResetEvent

In my last post, I discussed building an asynchronous version of a manual-reset event.  This time, we’ll build an asynchronous version of an auto-reset event.A manual-reset event is transitioned to the signaled state when requested to do so (i.e. calling Set()), and then it remains in that state until it’s manually ...

Building Async Coordination Primitives, Part 1: AsyncManualResetEvent

The Task-based Async Pattern (TAP) isn’t just about asynchronous operations that you initiate and then asynchronously wait for to complete.  More generally, tasks can be used to represent all sorts of happenings, enabling you to await for any matter of condition to occur.  We can even use Tasks to build simple coordination ...

Potential pitfalls to avoid when passing around async lambdas

One of the really useful capabilities of the new async methods feature in C# and Visual Basic is the ability to write async lambdas and anonymous methods (from here on in this post, I’ll refer to both of these as async lambdas, since the discussion applies equally to both).  This allows you to easily get a delegate to represent an ...

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 ...

FromAsync(asyncResult, …) vs FromAsync(beginMethod, …)

The Task Parallel Library (TPL) provides a set of “FromAsync” helper methods that create a Task or a Task<TResult> to represent an invocation of an APM method pair, i.e. BeginXx / EndXx.  There are, however, two different flavors among these overloads: ones that accept an IAsyncResult “asyncResult” as 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 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&rsquo...

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 ...

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 ...