Call asynchronization method and TaskScheduler in async / wait

Consider the following code

async Task<int> foo()
{
    await Task.Delay(1000);
    return 42; 
}

...
// OPTION 1

Task t = Task.Factory.StartNew(foo,
            CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

t.Wait();


...
// OPTION 2
Task t = foo();
t.Wait()

Questions

  • What is the significant difference between the two call options?

  • In option 1. Assume that I override the default TaskScheduler. To wait in the foo method - which TaskScheduler will be used? Will he use the default value or the value specified in the parameter to specify the father?

+4
source share
1 answer

, , " 1" , foo(), Task<Task<int>>. .Wait() , , ( Task.Delay).

TaskScheduler, , , , . , , , .

, , , , .

+3

Source: https://habr.com/ru/post/1547057/


All Articles