Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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