What is Task.RunSynchronously for?

18,232

Solution 1

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.

Solution 2

First let's have a look into this code:

public async static Task<int> MyAsyncMethod()
{
   await Task.Delay(100);
   return 100;
}

//Task.Delay(5000).RunSynchronously();                        // bang
//Task.Run(() => Thread.Sleep(5000)).RunSynchronously();     // bang
// MyAsyncMethod().RunSynchronously();                      // bang

var t = new Task(() => Thread.Sleep(5000));
t.RunSynchronously();                                     // works

In this example we've tried to invoke RunSynchronously on task that:

  • Returns other Task with result (promise task)
  • A 'hot' delegate task that runs the on a threadpool thread
  • Another promise task created by async await
  • 'Cold' task with a delegate

What statuses will they have after creation ?

  • WaitingForActivation
  • WaitingToRun
  • WaitingForActivation
  • Created

All 'hot' and promise tasks are created with status WaitingForActivationor WaitingToRun. Hot tasks are also associated with a task scheduler.

Method RunSynchronously only knows how to work with 'cold' tasks that contain delegate and has status Created.

Conclusion:

Method RunSynchronously had probably appeared when there were no 'hot' tasks or they hadn't been extensively used and was created for a specific purpose.

We might want to use it in case when we need a 'cold' task with custom TaskScheduler, otherwise it is outdated and pretty useless.

For running a 'hot' task synchronously (which we should avoid mostly) we could use task.GetAwaiter().GetResult(). This works same as .Result but as a bonus it returns original exception instead of AggregateException. Still, 'sync over async' is not the best choice and should be avoided if possible.

Share:
18,232
ValidfroM
Author by

ValidfroM

Updated on July 29, 2022

Comments

  • ValidfroM
    ValidfroM over 1 year

    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?