FAQ :: StartNew() with TaskScheduler.FromCurrentSynchronizationContext() doesn’t work?

We’ve seen a number of folks write the following code to execute on the UI thread and get unexpected behavior.   TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();Task uiTask = Task.Factory.StartNew(delegate{    // … Update UI component; BUG!}, uiScheduler); The issue is that the StartNew call will bind to the following overload…   public Task StartNew(Action<Object> action, Object state);… Read more

FAQ :: TaskScheduler.UnobservedTaskException event doesn’t work?

Recall that if exceptions thrown from Task bodies are left unobserved, they will be escalated.  In .NET 4, this means that TPL will throw them on the finalizer after the Task objects are available for garbage collection.  The UnobservedTaskException event on the TaskScheduler class was added as a last-resort method to observe such exceptions and… Read more

FAQ :: Parallel.ForEach and non-generic collections?

.NET 2.0 introduced Generics to allow enhanced code reusability and type safety.  Since then, generic collections (IEnumerable<T>, List<T>, Dictionary<T>, etc.) have become standard and are recommended over their non-generic counterparts (IEnumerable, ArrayList, HashTable, etc.).  As a result, Parallel.ForEach only supports generic collections, so code like the following will fail to compile.   IEnumerable nonGenericCollection =… Read more

FAQ :: Which .NET language is best for parallelism?

The new parallelization support in the .NET Framework 4 is implemented purely in libraries and the runtime and does not require special compiler support.  Therefore, it is available to all compliant .NET languages.  This includes all of the managed languages that ship as part of Visual Studio 2010 (Visual C#, Visual Basic, Visual F#, and… Read more

FAQ :: Are all of the new concurrent collections lock-free?

(This answer is based on the .NET Framework 4.  As the details below are undocumented implementation details, they may change in future releases.) No.  All of the collections in the new System.Collections.Concurrent namespace employ lock-free techniques to some extent in order to achieve general performance benefits, but traditional locks are used in some cases. It’s… Read more

FAQ :: You talk about performance, speedup, and efficiency…what do you mean exactly?

All of these terms are overloaded, even in the context of parallel computing.  However, we’ve used them extensively to describe how well our parallel algorithms and demo applications work.  And sometimes, we throw them around carelessly on the blog, forums, etc., so here are our general definitions. Performance is an attribute that refers to the… Read more

FAQ :: The Debugger does not correctly handle Task exceptions?

The following code correctly observes and handles a Task exception and should print “gotcha!” to the console.  By default though, the Debugger will report a crash.Task t = Task.Factory.StartNew(() => { throw new Exception(“poo”); }); try { t.Wait(); } catch (AggregateException) { Console.WriteLine(“gotcha!”); } The issue has to do with the “Just My Code” mode… Read more

FAQ :: Why is the speedup not X on my X-way machine?

We’ll be regularly posting answers to frequently asked questions that we’ve gotten on the forum, internal email lists, etc.  Here’s the first – enjoy! Why is the speedup not X on my X-way machine?  Or, why does my parallel code run slower?  Less than ideal speedup can typically be attributed to two things: 1.       Sequential… Read more