[英]async and await without “threads”? Can I customize what happens under-the-hood?
[英]How I can see the Async state machine (async/await under the hood) with the help of DotPeek?
我正在观看视频课程,其中作者在幕后谈论 async/await。 他展示了准备好的和反编译的代码。 我想从头开始做同样的事情。 我的意思是,在例如 DotPeek 的帮助下反编译一些 C# 编译文件。 所以我有以下简单的例子:
class Program
{
public static async Task KekAsync()
{
Console.WriteLine("Current thread id before await {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(200);
Console.WriteLine("Current thread id after await {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(200);
Console.WriteLine("Current thread id after await again {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(200);
Console.WriteLine("Current thread id after await again and again {0}", Thread.CurrentThread.ManagedThreadId);
}
static async Task Main(string[] args)
{
await KekAsync();
}
}
在 DotPeek 设置中,我有以下内容:
DotPeek 只是向我展示了我的源代码。 但我想看到异步状态机的实现。 代码生成结果。 我使用了 .NET Core 3.1 和最新版本的 .NET Framework。 两者都给我相同的结果。 我错过了什么吗?
由于名称中的 < 和 > 字符无效,因此生成的源代码将无法编译。
但是在 dnSpy 的帮助下,您可以重命名这些生成的类、字段和方法,以获得可编译的源代码以供进一步调查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.