Created
June 5, 2021 03:44
-
-
Save aaronmbos/a5247b2d7301fc7833af9b23036315a6 to your computer and use it in GitHub Desktop.
Code for Asynchronous C# Below the Surface Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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