using System;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
Task task = Task.Run(async () =>
{
await Task.Delay(2000);
Console.WriteLine("Async operation completed.");
});
Console.WriteLine("Main thread continues.");
task.Wait();
}
}
有限状态机
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
internal class Program
{
[Serializable]
[CompilerGenerated]
private sealed class <>c
{
[StructLayout(LayoutKind.Auto)]
private struct <<Main>b__0_0>d : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder <>t__builder;
private TaskAwaiter <>u__1;
private void MoveNext()
{
int num = <>1__state;
try
{
TaskAwaiter awaiter;
if (num != 0)
{
awaiter = Task.Delay(2000).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
return;
}
}
else
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter);
num = (<>1__state = -1);
}
awaiter.GetResult();
Console.WriteLine("Async operation completed.");
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult();
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine([Nullable(1)] IAsyncStateMachine stateMachine)
{
<>t__builder.SetStateMachine(stateMachine);
}
void IAsyncStateMachine.SetStateMachine([Nullable(1)] IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
public static readonly <>c <>9 = new <>c();
[Nullable(new byte[] { 0, 2 })]
public static Func<Task> <>9__0_0;
[NullableContext(2)]
[AsyncStateMachine(typeof(<<Main>b__0_0>d))]
internal Task <Main>b__0_0()
{
<<Main>b__0_0>d stateMachine = default(<<Main>b__0_0>d);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
[NullableContext(1)]
private static void Main(string[] args)
{
Task task = Task.Run(<>c.<>9__0_0 ?? (<>c.<>9__0_0 = new Func<Task>(<>c.<>9.<Main>b__0_0)));
Console.WriteLine("Main thread continues.");
task.Wait();
}
}
// coreclr trunk-20240410+89aae24966983366621c6dde6f79b8ae129567c4
Program:.ctor():this (FullOpts):
G_M48589_IG01: ;; offset=0x0000
G_M48589_IG02: ;; offset=0x0000
ret
Program:Main(System.String[]) (FullOpts):
G_M24006_IG01: ;; offset=0x0000
push rbp
push rbx
push rax
lea rbp, [rsp+0x10]
G_M24006_IG02: ;; offset=0x0008
test byte ptr [(reloc 0x7f2b9e9185fa)], 1 ; global ptr
je G_M24006_IG07
G_M24006_IG03: ;; offset=0x0015
mov rdi, 0x7EEB88001CE8 ; data for Program+<>c:<>9__0_0
mov rdi, gword ptr [rdi]
test rdi, rdi
jne SHORT G_M24006_IG05
G_M24006_IG04: ;; offset=0x0027
mov rdi, 0x7F2B9E971DA0 ; System.Func`1[System.Threading.Tasks.Task]
call CORINFO_HELP_NEWSFAST
mov rbx, rax
mov rdi, 0x7EEB88001CE0 ; data for Program+<>c:<>9
mov rsi, gword ptr [rdi]
test rsi, rsi
je SHORT G_M24006_IG08
lea rdi, bword ptr [rbx+0x08]
call CORINFO_HELP_ASSIGN_REF
mov rdi, 0x7F2B9E928F78 ; code for Program+<>c:<Main>b__0_0():System.Threading.Tasks.Task:this
mov qword ptr [rbx+0x18], rdi
mov rdi, 0x7EEB88001CE8 ; data for Program+<>c:<>9__0_0
mov rsi, rbx
call CORINFO_HELP_ASSIGN_REF
mov rdi, rbx
G_M24006_IG05: ;; offset=0x0077
xor rsi, rsi
call [System.Threading.Tasks.Task:Run(System.Func`1[System.Threading.Tasks.Task],System.Threading.CancellationToken):System.Threading.Tasks.Task]
mov rbx, rax
mov rdi, 0x7F2B8DEE5680 ; 'Main thread continues.'
call [System.Console:WriteLine(System.String)]
cmp byte ptr [rbx], bl
mov rdi, rbx
xor rdx, rdx
mov esi, -1
call [System.Threading.Tasks.Task:Wait(int,System.Threading.CancellationToken):ubyte:this]
nop
G_M24006_IG06: ;; offset=0x00A5
add rsp, 8
pop rbx
pop rbp
ret
G_M24006_IG07: ;; offset=0x00AC
mov rdi, 0x7F2B9E9185C8
mov esi, 2
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
jmp G_M24006_IG03
G_M24006_IG08: ;; offset=0x00C5
call [System.MulticastDelegate:ThrowNullThisInDelegateToInstance()]
int3
Program+<>c:.cctor() (FullOpts):
G_M6648_IG01: ;; offset=0x0000
push rax
G_M6648_IG02: ;; offset=0x0001
mov rdi, 0x7F2B9E91F6B8 ; Program+<>c
call CORINFO_HELP_NEWSFAST
mov rdi, 0x7EEB88001CE0 ; data for Program+<>c:<>9
mov rsi, rax
call CORINFO_HELP_ASSIGN_REF
nop
G_M6648_IG03: ;; offset=0x0023
add rsp, 8
ret
Program+<>c:.ctor():this (FullOpts):
G_M39655_IG01: ;; offset=0x0000
G_M39655_IG02: ;; offset=0x0000
ret
Program+<>c:<Main>b__0_0():System.Threading.Tasks.Task:this (FullOpts):
G_M11611_IG01: ;; offset=0x0000
push rbp
sub rsp, 32
lea rbp, [rsp+0x20]
vxorps xmm8, xmm8, xmm8
vmovdqu xmmword ptr [rbp-0x18], xmm8
xor eax, eax
mov qword ptr [rbp-0x08], rax
G_M11611_IG02: ;; offset=0x001A
mov dword ptr [rbp-0x18], -1
lea rdi, [rbp-0x18]
call [System.Runtime.CompilerServices.AsyncMethodBuilderCore:Start[Program+<>c+<<Main>b__0_0>d](byref)]
mov rax, gword ptr [rbp-0x10]
test rax, rax
je SHORT G_M11611_IG04
G_M11611_IG03: ;; offset=0x0034
add rsp, 32
pop rbp
ret
G_M11611_IG04: ;; offset=0x003A
lea rdi, [rbp-0x10]
call [System.Runtime.CompilerServices.AsyncTaskMethodBuilder:InitializeTaskAsPromise():System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]:this]
jmp SHORT G_M11611_IG03
Program+<>c+<<Main>b__0_0>d:MoveNext():this (FullOpts):
G_M43724_IG01: ;; offset=0x0000
push rbp
push r15
push r14
push r13
push rbx
sub rsp, 32
lea rbp, [rsp+0x40]
xor eax, eax
mov qword ptr [rbp-0x28], rax
mov qword ptr [rbp-0x40], rsp
mov bword ptr [rbp-0x38], rdi
G_M43724_IG02: ;; offset=0x001F
mov eax, dword ptr [rdi]
G_M43724_IG03: ;; offset=0x0021
test eax, eax
je G_M43724_IG06
test byte ptr [(reloc 0x7f2b9e7452c0)], 1 ; global ptr
je SHORT G_M43724_IG05
G_M43724_IG04: ;; offset=0x0032
mov rax, 0x7EEB88000398 ; data for System.TimeProvider:<System>k__BackingField
mov rbx, gword ptr [rax]
mov rdi, 0x7F2B9E9793D8 ; System.Threading.Tasks.Task+DelayPromise
call CORINFO_HELP_NEWSFAST
mov r15, rax
mov rdi, r15
mov rdx, rbx
mov esi, 0x7D0
call [System.Threading.Tasks.Task+DelayPromise:.ctor(uint,System.TimeProvider):this]
mov gword ptr [rbp-0x28], r15
mov rdi, gword ptr [rbp-0x28]
test dword ptr [rdi+0x34], 0x1600000
jne SHORT G_M43724_IG07
xor edi, edi
mov rbx, bword ptr [rbp-0x38]
mov dword ptr [rbx], edi
lea rdi, bword ptr [rbx+0x10]
mov rsi, gword ptr [rbp-0x28]
call CORINFO_HELP_CHECKED_ASSIGN_REF
lea rsi, bword ptr [rbx+0x08]
mov rdi, rbx
call [System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[System.Threading.Tasks.VoidTaskResult]:GetStateMachineBox[Program+<>c+<<Main>b__0_0>d](byref,byref):System.Runtime.CompilerServices.IAsyncStateMachineBox]
mov rsi, rax
lea rdi, [rbp-0x28]
call [System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[System.Threading.Tasks.VoidTaskResult]:AwaitUnsafeOnCompleted[System.Runtime.CompilerServices.TaskAwaiter](byref,System.Runtime.CompilerServices.IAsyncStateMachineBox)]
jmp G_M43724_IG13
G_M43724_IG05: ;; offset=0x00A7
mov rdi, 0x7F2B9E7450C0
mov esi, 464
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
jmp G_M43724_IG04
G_M43724_IG06: ;; offset=0x00C0
mov rdi, bword ptr [rbp-0x38]
mov rsi, gword ptr [rdi+0x10]
mov gword ptr [rbp-0x28], rsi
xor esi, esi
mov qword ptr [rdi+0x10], rsi
mov dword ptr [rdi], -1
G_M43724_IG07: ;; offset=0x00D8
mov rsi, gword ptr [rbp-0x28]
mov eax, dword ptr [rsi+0x34]
and eax, 0x11000000
cmp eax, 0x1000000
je SHORT G_M43724_IG09
G_M43724_IG08: ;; offset=0x00EB
mov rdi, rsi
xor esi, esi
call [System.Runtime.CompilerServices.TaskAwaiter:HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task,int)]
G_M43724_IG09: ;; offset=0x00F6
mov rdi, 0x7F2B8DEE56F0 ; 'Async operation completed.'
call [System.Console:WriteLine(System.String)]
nop
G_M43724_IG10: ;; offset=0x0107
mov rbx, bword ptr [rbp-0x38]
mov dword ptr [rbx], -2
add rbx, 8
cmp gword ptr [rbx], 0
jne SHORT G_M43724_IG15
G_M43724_IG11: ;; offset=0x011B
test byte ptr [(reloc 0x7f2b9e7454ea)], 1 ; global ptr
je G_M43724_IG23
G_M43724_IG12: ;; offset=0x0128
mov rdi, 0x7EEB88000868 ; data for System.Threading.Tasks.Task:s_cachedCompleted
mov rsi, gword ptr [rdi]
mov rdi, rbx
call CORINFO_HELP_CHECKED_ASSIGN_REF
G_M43724_IG13: ;; offset=0x013D
nop
G_M43724_IG14: ;; offset=0x013E
add rsp, 32
pop rbx
pop r13
pop r14
pop r15
pop rbp
ret
G_M43724_IG15: ;; offset=0x014B
mov r15, gword ptr [rbx]
test byte ptr [(reloc 0x7f2b9e745528)], 1 ; global ptr
je G_M43724_IG24
G_M43724_IG16: ;; offset=0x015B
mov rax, 0x7EEB88000960 ; data for System.Threading.Tasks.TplEventSource:Log
mov rax, gword ptr [rax]
cmp byte ptr [rax+0x9D], 0
jne G_M43724_IG25
G_M43724_IG17: ;; offset=0x0175
mov eax, dword ptr [r15+0x34]
mov dword ptr [rbp-0x2C], eax
test eax, 0x5600000
jne SHORT G_M43724_IG22
G_M43724_IG18: ;; offset=0x0183
lea r14, bword ptr [r15+0x34]
mov edi, eax
or edi, 0x4000000
lock
cmpxchg dword ptr [r14], edi
cmp eax, dword ptr [rbp-0x2C]
jne G_M43724_IG27
jmp SHORT G_M43724_IG20
G_M43724_IG19: ;; offset=0x019F
test eax, eax
je SHORT G_M43724_IG22
G_M43724_IG20: ;; offset=0x01A3
mov byte ptr [r15+0x38], 0
mov edi, dword ptr [r15+0x34]
or edi, 0x1000000
xchg dword ptr [r14], edi
mov r13, gword ptr [r15+0x28]
test r13, r13
jne G_M43724_IG26
G_M43724_IG21: ;; offset=0x01C2
mov rdi, r15
call [System.Threading.Tasks.Task:FinishContinuations():this]
jmp G_M43724_IG13
G_M43724_IG22: ;; offset=0x01D0
mov edi, 24
call [System.ThrowHelper:ThrowInvalidOperationException(int)]
int3
G_M43724_IG23: ;; offset=0x01DC
mov rdi, 0x7F2B9E7450C0
mov esi, 0x3FA
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
jmp G_M43724_IG12
G_M43724_IG24: ;; offset=0x01F5
mov rdi, 0x7F2B9E7450C0
mov esi, 0x438
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
jmp G_M43724_IG16
G_M43724_IG25: ;; offset=0x020E
mov rdi, 0x7F2B9E7450C0
mov esi, 0x438
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
mov rdi, 0x7EEB88000960 ; data for System.Threading.Tasks.TplEventSource:Log
mov r14, gword ptr [rdi]
mov rdi, r15
cmp dword ptr [rdi], edi
call [System.Threading.Tasks.Task:get_Id():int:this]
mov esi, eax
mov rdi, r14
mov edx, 1
cmp dword ptr [rdi], edi
call [System.Threading.Tasks.TplEventSource:TraceOperationEnd(int,int):this]
jmp G_M43724_IG17
G_M43724_IG26: ;; offset=0x0251
mov rdi, r15
call [System.Threading.Tasks.Task:NotifyParentIfPotentiallyAttachedTask():this]
mov rdi, r13
call [System.Threading.Tasks.Task+ContingentProperties:SetCompleted():this]
jmp G_M43724_IG21
G_M43724_IG27: ;; offset=0x0268
mov rdi, r15
mov esi, 0x4000000
mov edx, 0x5600000
call [System.Threading.Tasks.Task:AtomicStateUpdateSlow(int,int):ubyte:this]
jmp G_M43724_IG19
G_M43724_IG28: ;; offset=0x0280
push rbp
push r15
push r14
push r13
push rbx
sub rsp, 16
mov rbp, qword ptr [rdi]
mov qword ptr [rsp], rbp
lea rbp, [rbp+0x40]
G_M43724_IG29: ;; offset=0x0297
mov rbx, bword ptr [rbp-0x38]
mov dword ptr [rbx], -2
lea rdi, bword ptr [rbx+0x08]
call [System.Runtime.CompilerServices.AsyncTaskMethodBuilder:SetException(System.Exception):this]
lea rax, G_M43724_IG13
G_M43724_IG30: ;; offset=0x02B2
add rsp, 16
pop rbx
pop r13
pop r14
pop r15
pop rbp
ret
Program+<>c+<<Main>b__0_0>d:SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine):this (FullOpts):
G_M39149_IG01: ;; offset=0x0000
push rax
G_M39149_IG02: ;; offset=0x0001
cmp byte ptr [rdi], dil
test rsi, rsi
je SHORT G_M39149_IG04
G_M39149_IG03: ;; offset=0x0009
add rsp, 8
ret
G_M39149_IG04: ;; offset=0x000E
mov edi, 61
call [System.ThrowHelper:ThrowArgumentNullException(int)]
int3