.NET Parallel Programming

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

Latest posts

New Task APIs in .NET 4.6
Feb 2, 2015
0
2

New Task APIs in .NET 4.6

Stephen Toub - MSFT
Stephen Toub - MSFT

There are several nice API-level enhancements to the Task Parallel Library in .NET 4.6, which you can grab a preview of as part of the Visual Studio 2015 CTP. Task.From* .NET 4.5 had a Task.FromResult method.  This method makes it quick and easy to manufacture a new Task for a known result value, and is particularly useful when implementing a Task-returning method to complete synchronously.  However, Task didn't expose corresponding methods for creating canceled or faulted tasks; instead, developers needed to manually create such tasks, such as by using a TaskCompletionSource, e.g. public static Task<TR...

.NET memory allocation profiling and Tasks
Apr 4, 2013
0
1

.NET memory allocation profiling and Tasks

Stephen Toub - MSFT
Stephen Toub - MSFT

The .NET Framework blog published this morning a guest post from yours truly on .NET Memory Allocation Profiling with Visual Studio 2012.  As you're trying to improve the performance, throughput, and memory usage of code that uses Tasks, the described profiler in Visual Studio can be a valuable tool in your tool belt (of course, the example I use in that post to highlight the profiler's capabilities is one that uses Task and async/await).  I hope you find the post helpful.

Tasks, Monads, and LINQ
Apr 3, 2013
4
1

Tasks, Monads, and LINQ

Stephen Toub - MSFT
Stephen Toub - MSFT

A few years back, Wes Dyer wrote a great post on monads, and more recently, Eric Lippert wrote a terrific blog series exploring monads and C#. In that series, Eric alluded to Task<TResult> several times, so I thought I’d share a few related thoughts on Task<TResult> and the async/await keywords.As both Wes and Eric highlight, a monad is a triple consisting of a type, a Unit function (often called Return), and a Bind function. If the type in question is Task<T>, what are its Unit and Bind functions? The Unit operator takes a T and “amplifies” it into an instance of the type: p...

“Invoke the method with await”… ugh!
Mar 13, 2013
0
2

“Invoke the method with await”… ugh!

Stephen Toub - MSFT
Stephen Toub - MSFT

I can be a bit sensitive when it comes to language and how concepts are conveyed.  I think it’s important to be accurate, even if not precise, when describing what something is or how to use it, as otherwise the folks to whom you’re communicating can easily form the wrong mental model for that thing.  Having a good mental model for something is important in then being able to reason about the thing in question and to correctly infer other uses and behaviors.That’s why I frequently cringe when I hear someone say something like “invoke the method using 'await'”.  The com...

MVP Summit presentation on async
Feb 20, 2013
1
0

MVP Summit presentation on async

Stephen Toub - MSFT
Stephen Toub - MSFT

Lucian Wischik and I presented an "async clinic" at the MVP Summit in Bellevue this week.  The async/await keywords in C# and Visual Basic drastically simplify asynchronous programming, but that of course doesn't mean that using them is without any gotchas: the goal of the discussion was to highlight some of the key areas in which we see developers struggling with asynchronous development and to help provide guidance on avoiding and overcoming those roadblocks.  Attached are the slides from the presentation. MVPSummit2013_AsyncClinic_Wischik_Toub.pptx

Psychic Debugging of Async Methods
Jan 28, 2013
0
0

Psychic Debugging of Async Methods

Stephen Toub - MSFT
Stephen Toub - MSFT

These days it’s not uncommon for me to receive an email or read a forum post from someone concerned about a problem they’re experiencing with an async method they’ve written, and they’re seeking help debugging the issue.  Sometimes plenty of information about the bug is conveyed, but other times the communication is void of anything more than a root problem statement.  That’s when I engage my powers of psychic debugging to suggest what the root cause might be, without actually knowing more about the developer’s codebase.  Here are four of the more common issues I...

Cooperatively pausing async methods
Jan 13, 2013
0
2

Cooperatively pausing async methods

Stephen Toub - MSFT
Stephen Toub - MSFT

Recently I was writing an app that processed a bunch of files asynchronously.  As with the Windows copy file dialog, I wanted to be able to provide the user with a button that would pause the processing operation.To achieve that, I implemented a simple mechanism that would allow me to pass a “pause token” into the async method, which the async method could asynchronous wait on at appropriate points.  public async Task ProcessFiles(     IEnumerable<StorageFile> files, PauseToken pauseToken) {     foreach(var file in files) ...

C# memory model articles
Dec 14, 2012
0
1

C# memory model articles

Stephen Toub - MSFT
Stephen Toub - MSFT

Igor Ostrovsky is one of the minds behind the parallel programming support in the .NET Framework.  Igor's recently written a great set of articles for MSDN Magazine to cover "The C# Memory Model in Theory and Practice".  Part 1 is available now in the December 2012 issue, and it's a great read.

PLINQ and Int32.MaxValue
Nov 16, 2012
0
0

PLINQ and Int32.MaxValue

Stephen Toub - MSFT
Stephen Toub - MSFT

In both .NET 4 and .NET 4.5, PLINQ supports enumerables with up to Int32.MaxValue elements.  Beyond that limit, PLINQ will throw an overflow exception.  LINQ to Objects itself has this limitation with certain query operators (such as the indexed Select operator which counts the elements processed), but PLINQ has it with more.This limitation impacts so few scenarios that it’s relatively benign and I rarely hear about it.  That said, it does come up now and again, and I was in fact asked about it earlier this week.  So, to help in case anyone else runs into this…There is a relativel...