Yiqun Cui
Yiqun Cui

Reputation: 55

Questions about multiple tasks in one async function

Here is my code to test the situation with multiple tasks in one async function:

class EvaluateAwait
    {
        
        static async Task Main()
        {
            Console.WriteLine("Main func: Thread ID = " + Thread.CurrentThread.ManagedThreadId);
            Task task1 = PrintAndWait("1");
            Console.WriteLine("Task 1 created");
            Task task2 = PrintAndWait("2");           
            Console.WriteLine("Task 2 Created");
            await task1; 
            await task2;
            Console.WriteLine("Main Returned.");
        }

        static async Task PrintAndWait(string id)
        {
            
            for (int i = 0; i < 50; i++) { 
                await Task.Delay(1000);
            
                Console.WriteLine("Thread ID:"+ Thread.CurrentThread.ManagedThreadId + " | Task ID:" +id + " | " );
            }

            Console.WriteLine("Thread ID: " + Thread.CurrentThread.ManagedThreadId + " | Task ID: " + id + "completed");

        }


    }


}

My origianl expecation for what happens is:

  1. Main func prints its thread
  2. Main func enters PrintAndWait for the first time for task1.

    PrintAndWait returns but not completed,when meeting the first await sentence await Task.Delay(5000).

  3. Main func prints "Task 1 created"
  4. Main func enters PrintAndWait for the second time for task2.

    PrintAndWait returns but not completed,when meeting the first await sentence await Task.Delay(5000)

  5. await task1 so task1 should return the awaiter, do its 50 loops to print thread ID every sec. Here comes my quetion, as await task1 is the first await sentence in Main, I suppose Main should return here, which means any of the code after this line, should not be run until task1 is finished. So I am expecting seeing task1 prints the thread info first, then task2 do the printing.
  6. await task2 so task2 should return the awaiter, do its 50 loops to print thread ID every sec.

But the output is

Main func: Thread ID = 1
Task 1 created
Task 2 Created
Thread ID:7 | Task ID:2 |
Thread ID:10 | Task ID:1 |
Thread ID:7 | Task ID:2 |
Thread ID:10 | Task ID:1 |

It looks like Task1 and Task2 running simultaneously. Do I have some misunderstandings about the await mechnism?

Upvotes: 0

Views: 116

Answers (1)

hatcyl
hatcyl

Reputation: 2352

It looks like Task1 and Task2 running simultaneously. Do I have some misunderstandings about the await mechnism?

As your tests showed, yes you do.

Task task1 = PrintAndWait("1");

That can start the code right away. When you use await it means that now you actually need the result, so "wait" for it.

If the code didn't start right away, what would be the point of:

Task task1 = PrintAndWait("1");
Console.WriteLine("Task 1 created");
Task task2 = PrintAndWait("2");           
Console.WriteLine("Task 2 Created");
await task1; 
await task2;

instead of?

await PrintAndWait("1");
await PrintAndWait("2");

Upvotes: 0

Related Questions