Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aaronmbos
Created June 5, 2021 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmbos/a5247b2d7301fc7833af9b23036315a6 to your computer and use it in GitHub Desktop.
Save aaronmbos/a5247b2d7301fc7833af9b23036315a6 to your computer and use it in GitHub Desktop.
Code for Asynchronous C# Below the Surface Post
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
[CompilerGenerated]
private sealed class DoSomethingAsync_Compiler : IAsyncStateMachine
{
public int state; // Represents the current state of the state machine
public AsyncTaskMethodBuilder builder; // Key to starting the state machine & propogating the result
public int input; // Input to the method
private TaskAwaiter awaiter1; // Important for keeping track of the awaiter on continuations
private void MoveNext()
{
int num = state;
try
{
TaskAwaiter awaiter;
if (num != 0)
{
Console.WriteLine("Before awaiting.");
awaiter = Task.Delay(input).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (state = 0);
awaiter1 = awaiter;
DoSomethingAsync_Compiler stateMachine = this;
builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = awaiter1;
awaiter1 = default(TaskAwaiter);
num = (state = -1);
}
awaiter.GetResult();
Console.WriteLine("Task has been awaited.");
}
catch (Exception exception)
{
state = -2;
builder.SetException(exception);
return;
}
state = -2;
builder.SetResult();
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment