What is Task.RunSynchronously for?

  • A+

I just wonder what's the method for? In what kind of scenario I can use this method.

My initial thought is RunSynchronously is for calling an async method and running that synchronously without causing a deadlock issue like what .wait() does.

However, according to MSDN,

Ordinarily, tasks are executed asynchronously on a thread pool thread and do not block the calling thread. Tasks executed by calling the RunSynchronously() method are associated with the current TaskScheduler and are run on the calling thread. If the target scheduler does not support running this task on the calling thread, the task will be scheduled for execution on the schedule, and the calling thread will block until the task has completed execution

Why need a TaskScheduler here, if the task going to run on the calling thread?


RunSynchronously delegates the decision of when to start the task to the current task scheduler (or the one passed as argument).

I am not sure why it is there (maybe for internal or legacy use), but it is hard to think of a useful use case in the current versions of .NET. @Fabjan has a possible explanation in his comment to the question.

RunSynchronously asks the scheduler to run it synchronously but then the scheduler could very well ignore the hint and run it in a thread pool thread and your current thread will synchronously block until it is completed.

The scheduler does not have to run it on the current thread and does not have to run it immediately although I think it is what will happen on common schedulers (ThreadPoolTaskScheduler and common UI schedulers).

RunSynchronously will also throw an exception if the task has already been started or is completed/faulted (this means you will not be able to use it on async methods).

This code may clarify the different behaviour:

Wait and Result don't run the task at all, they just wait for the task completion on the current thread and block it until the completion so if we want to compare, we can compare Start and Wait to RunSynchronously:

class Scheduler : TaskScheduler {     protected override void QueueTask(Task task) =>          Console.WriteLine("QueueTask");      protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)     {         Console.WriteLine("TryExecuteTaskInline");          return false;     }      protected override IEnumerable<Task> GetScheduledTasks() => throw new NotImplementedException(); }  static class Program {     static void Main()     {         var taskToStart = new Task(() => { });         var taskToRunSynchronously = new Task(() => { });          taskToStart.Start(new Scheduler());         taskToRunSynchronously.RunSynchronously(new Scheduler());     } } 

If you try and comment Start or RunSynchronously and run the code, you will see that Start tries and queue the task to the scheduler while RunSynchronously will try and execute it inline and if failing (return false), it will just queue it.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: