1

I tried the following in a console application.

static async Task Main(string[] args)
{
    var sw = Stopwatch.StartNew();
    Console.WriteLine("Main");

    var task1 = Method1(sw);
    var task2 = Method2(sw);

    await task1;
    await task2;
    //await Task.WhenAll(task1, task2); // results in the same behavior
}

static Task<bool> Method1(Stopwatch sw)
{
    Thread.Sleep(3000);
    Console.WriteLine("Method1 " + sw.ElapsedMilliseconds);
    return Task.FromResult(true);
}

static Task<bool> Method2(Stopwatch sw)
{
    Thread.Sleep(3000);
    Console.WriteLine("Method2 " + sw.ElapsedMilliseconds);
    return Task.FromResult(false);
}

I expected Method1 and Method2 to run in parallel and print something like

Main
Method1 3047
Method2 3052

But the actual output came as

Main
Method1 3047
Method2 6052

What is wrong here?

aknag
  • 63
  • 5

0 Answers0