Skip to content

Instantly share code, notes, and snippets.

@vasilkosturski
Last active January 1, 2021 13:42
Show Gist options
  • Save vasilkosturski/177e545417ffa8b78cdae42afcd31613 to your computer and use it in GitHub Desktop.
Save vasilkosturski/177e545417ffa8b78cdae42afcd31613 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
public static class MyClass
{
[StructLayout(LayoutKind.Auto)]
[CompilerGenerated]
private struct <MyAsyncMethod>d__0 : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder<int> <>t__builder;
public int firstDelay;
public int secondDelay;
private TaskAwaiter <>u__1;
void IAsyncStateMachine.MoveNext()
{
int num = <>1__state;
int result;
try
{
TaskAwaiter awaiter;
if (num != 0)
{
if (num == 1)
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter);
num = (<>1__state = -1);
goto IL_00db;
}
Console.WriteLine("Before first await.");
awaiter = Task.Delay(firstDelay).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("Before second await.");
awaiter = Task.Delay(secondDelay).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 1);
<>u__1 = awaiter;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
return;
}
goto IL_00db;
IL_00db:
awaiter.GetResult();
Console.WriteLine("Done.");
result = 42;
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult(result);
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
<>t__builder.SetStateMachine(stateMachine);
}
}
[AsyncStateMachine(typeof(<MyAsyncMethod>d__0))]
public static Task<int> MyAsyncMethod(int firstDelay, int secondDelay)
{
<MyAsyncMethod>d__0 stateMachine = default(<MyAsyncMethod>d__0);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
stateMachine.firstDelay = firstDelay;
stateMachine.secondDelay = secondDelay;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment