Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

RunContinuationsAsynchronously does not run continuations asynchronously


View products that this article applies to.

Symptoms

The .NET Framework 4.6 added a new enum type value, RunContinuationsAsynchronously, to the TaskCompletionSource and TaskCreationOptions enums. This value forces tasks to run asynchronously, and this helps to avoid deadlock situations. However, an issue has been discovered where, for some specific kinds of continuations (in particular when Task.WhenAll, Task.WhenAny, or TaskExtensions.Unwrap create the continuations), the option is ignored. Therefore, continuations may still run synchronously.

The following sample shows the problem. If the RunContinuationsAsynchronously value were functioning correctly, the two thread IDs that are printed out would never be the same, because the continuation would always be scheduled onto different threads.

using System;
using System.Threading;
using System.Threading.Tasks;

class Program
{
static void Main()
{
var mres = new ManualResetEventSlim();

Console.WriteLine(Environment.CurrentManagedThreadId);

var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

var t = Task.WhenAll(tcs.Task);

t.ContinueWith(delegate
{
Console.WriteLine(Environment.CurrentManagedThreadId);
mres.Set();
}, TaskContinuationOptions.ExecuteSynchronously);

tcs.SetResult(true);
mres.Wait();
}
}

↑ Back to the top


Resolution

There is currently no workaround for this issue. The Microsoft .NET Framework team is working on a solution to include in a future update. 


↑ Back to the top


Keywords: kbsurveynew, kbexpertiseadvanced, kbtshoot, kb

↑ Back to the top

Article Info
Article ID : 3118695
Revision : 1
Created on : 1/7/2017
Published on : 11/24/2015
Exists online : False
Views : 532