I read the Microsoft document about async and await. It says:
The async and await keywords don't cause additional threads to be created.
But I run the below code
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class AsyncMain
{
static void Main()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Task task = Test();
task.Wait();
}
public async static Task Test()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
await Task.Delay(1000);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
}
}
The result is
1
1
4
It is obvious that the
await Task.Delay(1000);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
is run in another thread. Why does the document says there is no thread created?
I have read the previous question Does the use of async/await create a new thread?, but it does not answer my question, the answer there just copy pastes from a Microsoft source. Why do I see different thread IDs in my test?