How I can see the Async state machine (async/await under the hood) with the help of DotPeek?

1.6k views Asked by At

I'm watching a video lesson, where the author talking about async/await under the hood. He shows prepared and decompiled code. I want to do the same from scratch. I mean, decompile some C# compiled file with the help of, from example, DotPeek. So I have the following simple example:

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

In the DotPeek settings I have the following:

enter image description here

But I don't see code generation result. I see async and await. enter image description here

DotPeek just shows me my source code. But I would like to see implementation of Async state machine. Result of the code generation. I used .NET Core 3.1 and the last version of .NET Framework. Both give me the same result. Am I miss something?

2

There are 2 answers

0
Aleksej_Shherbak On

So, the answer is simple. Right click on the file -> Decompiled Sources

enter image description here

0
Merilix2 On

Because < and > characters within names are invalid the generated source will not compile.

But with the help of dnSpy you can rename those generated classes, fields and methods to get compilable source code for further investigation.