小编HAO*_*HAO的帖子

Does await create another thread?

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);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

The result is

1
1
4

It is obvious that the

await …
Run Code Online (Sandbox Code Playgroud)

.net c# async-await

1
推荐指数
1
解决办法
194
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1