Task continuation was not scheduled on thread-pool thread

692 views Asked by At

I was reading about SynchronizationContext and its use with the async/await methods (link). From my understanding, in a Console application where the SynchronizationContext is null, the continuation of an awaited method (Task) will be scheduled with the default scheduler which would be the ThreadPool.

But if I run this console app, you'll see from the output that the the continuation is run on the worker thread that I created:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("MainThreadId=" + Thread.CurrentThread.ManagedThreadId);
        Method1().ContinueWith(t =>
        {
            Console.WriteLine("After Method1. ThreadId=" + Thread.CurrentThread.ManagedThreadId);
        });

        Console.ReadKey();
    }

    public static async Task Method1()
    {
        Console.WriteLine("Method1 => Entered. ThreadId=" + Thread.CurrentThread.ManagedThreadId);

        TaskCompletionSource<bool> completionSource = new TaskCompletionSource<bool>();
        Thread thread = new Thread(() =>
        {
            Console.WriteLine("Method1 => Started new thread. ThreadId=" + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(2000);
            completionSource.SetResult(true);
        });

        thread.Start();

        await completionSource.Task;

        Console.WriteLine("Method1 => After WorkerThread. ThreadId=" + Thread.CurrentThread.ManagedThreadId);
    }
}

And here is the output:

MainThreadId=10
Method1 => Entered. ThreadId=10
Method1 => Started new thread. ThreadId=11
Method1 => After WorkerThread. ThreadId=11
After Method1. ThreadId=12

As you can see, "After WorkerThread" was outputted on the same thread as my workerthread, but not on the threadpool.

I found a similar question but the guy was using Mono and they were saying that this was a bug. On my side, I built this code with in Visual Studio and ran it under Windows 7 and .Net 4.5.2 installed on my machine.

Could someone please explain this behaviour?

1

There are 1 answers

6
Stephen Cleary On BEST ANSWER

It's because of an implementation detail that I documented on my blog: the continuation created by await is scheduled using the ExecuteSynchronously flag. In that case, when it is time to fire the continuation (i.e., in the TaskCompletionSource<T>.SetResult call on the worker thread), the default scheduler first attempts to determine whether it can run on the current thread.

Since the worker thread has no TaskScheduler that will reject executing the task synchronously, the ExecuteSynchronously flag will cause the thread pool task scheduler to just execute the task synchronously (i.e., on the calling thread).