.NET Parallel Programming
All about Async/Await, System.Threading.Tasks, System.Collections.Concurrent, System.Linq, and more…
Latest posts
How do I cancel non-cancelable async operations?
This is a question I hear relatively frequently: “I have an async operation that’s not cancelable. How do I cancel it?” The construction of the question often makes me chuckle, but I understand and appreciate what’s really being asked. The developer typically isn’t asking how to cancel the operation itself (if they are asking that, the answer is simple: it’s not cancelable!), but rather they’re asking how to allow their program’s execution to continue upon a cancellation request even if the operation being waited on hasn’t completed yet. T...
New TaskCreationOptions and TaskContinuationOptions in .NET 4.5
Astute users of the Task Parallel Library might have noticed three new options available across TaskCreationOptions and TaskContinuationOptions in .NET 4.5: DenyChildAttach, HideScheduler, and (on TaskContinuationOptions) LazyCancellation. I wanted to take a few minutes to share more about what these are and why we added them.DenyChildAttachAs a reminder, when a Task is created with TaskCreationOptions.AttachedToParent or TaskContinuationOptions.AttachedToParent, the creation code looks to see what task is currently running on the current thread (this Task’s Id is available from the static Task.Curren...
Forking in async methods
Given that .NET 4.5 has only recently been released in its final form, it’s not surprising that many folks are still very new to the async/await keywords and have misconceptions about what they are and what they do (I’ve tried to clarify some of these in this Async/Await FAQ). One of the more common misconceptions is that a method marked as async forces asynchrony, and that’s not true: if there are no awaits in a method marked as async, or if none of the awaits in the async method ever result in suspensions (because they awaited awaitables that were already complete by the time the await o...
TPL Dataflow NuGet Package for .NET 4.5
As just announced on the Base Class Libraries (BCL) team blog, the RTM release of TPL Dataflow is now available.Enjoy :)
Implementing Then with Await
In a post a while ago, I talked about sequential composition of asynchronous operations. Now that we have the async/await keywords in C# and Visual Basic, such composition is trivial, and async/await are indeed the recommended way to achieve such composition with these languages.However, in that post I also described a few “Then” methods (similar methods are now available in JavaScript and VC++), and I thought it’d be fun to quickly show how Then can be implemented in terms of await. We’d probably want to support Then methods that operate on either Task or Task<TResult>, and th...
Processing tasks as they complete
Recently I’ve had several folks ask me about how to process the results of tasks as those tasks complete.A developer will have multiple tasks representing asynchronous operations they’ve initiated, and they want to process the results of these tasks, e.g. List<Task<T>> tasks = …; foreach(var t in tasks) { try { Process(await t); } catch(OperationCanceledException) {} catch(Exception exc) { Handle(exc); } } This approach is fine for many situations. However, it enforces an additional co...
ExecutionContext vs SynchronizationContext
I’ve been asked a few times recently various questions about ExecutionContext and SynchronizationContext, for example what the differences are between them, what it means to “flow” them, and how they relate to the new async/await keywords in C# and Visual Basic. I thought I’d try to tackle some of those questions here.WARNING: This post goes deep into an advanced area of .NET that most developers never need to think about.What is ExecutionContext, and what does it mean to flow it?ExecutionContext is one of those things that the vast majority of developers never need to think about.&n...
Using async/await in WinRT async operations
Several weeks ago, I wrote a post for the Windows 8 app developer blog that was all about using await and AsTask to consume WinRT async operations. I've now published a follow-up post that's all about exposing .NET tasks as WinRT async operation. In a sense, you can think about the first post as showing how to convert from WinRT async operations to .NET tasks, and you can think about the second post as showing how to convert from .NET tasks to WinRT async operations. I hope you find them helpful.
Performance consideration for Async/Await and MarshalByRefObject
In the previous "What's New for Parallelism in Visual Studio 2012 RC" blog post, I mentioned briefly that for the .NET 4.5 Release Candidate, StreamReader.ReadLineAsync experienced a significant performance improvement over Beta. There's an intriguing story behind that, one I thought I'd share here.It has to do with some interesting interactions between certain optimizations in the BCL and how the C# and Visual Basic compilers compile async methods. Before I describe the changes made, a bit of experimentation will be useful to help set the stage. Consider the following code. The code is simp...