![](/img/trans.png)
[英]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.