Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Decompiled with JetBrains decompiler
// Type: AsyncAwaitTest.Program
// Assembly: AsyncAwaitTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 52DF839B-9B18-40C2-9E30-2E3827E07665
// Assembly location: C:\code\AsyncAwaitTest\AsyncAwaitTest\bin\Debug\netcoreapp3.1\AsyncAwaitTest.dll
// Compiler-generated code is shown
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwaitTest
{
internal class Program
{
private static void Main(string[] args)
{
Program.PrintWithThreadID("Starting Program");
Console.WriteLine();
Console.WriteLine("____Got Result: " + Program.ReadFileHierarchy("file1a"));
Console.WriteLine();
// ISSUE: method pointer
Console.WriteLine("____Got Simple Task Result: " + Task.Run<string>(Program.\u003C\u003Ec.\u003C\u003E9__0_0 ?? (Program.\u003C\u003Ec.\u003C\u003E9__0_0 = new Func<string>((object) Program.\u003C\u003Ec.\u003C\u003E9, __methodptr(\u003CMain\u003Eb__0_0)))).GetAwaiter().GetResult());
Console.WriteLine();
Task<string>[] taskArray = new Task<string>[2]
{
Program.ReadFileHierarchyAsync("file1a"),
Program.ReadFileHierarchyAsync("file1b")
};
foreach (string str in Task.WhenAll<string>(taskArray).GetAwaiter().GetResult())
Console.WriteLine("____Combined Task Result: " + str);
}
private static string ReadFileHierarchy(string fileOneName)
{
Program.PrintWithThreadID("Starting Read");
fileOneName += ".txt";
string path1 = File.ReadAllText(fileOneName) + ".txt";
Program.PrintWithThreadID("Got Second File Name: " + path1);
string path2 = File.ReadAllText(path1) + ".txt";
Program.PrintWithThreadID("Got Third File Name: " + path2);
return File.ReadAllText(path2);
}
[AsyncStateMachine(typeof (Program.\u003CReadFileHierarchyAsync\u003Ed__2))]
[DebuggerStepThrough]
private static Task<string> ReadFileHierarchyAsync(string fileOneName)
{
Program.\u003CReadFileHierarchyAsync\u003Ed__2 stateMachine = new Program.\u003CReadFileHierarchyAsync\u003Ed__2();
stateMachine.\u003C\u003Et__builder = AsyncTaskMethodBuilder<string>.Create();
stateMachine.fileOneName = fileOneName;
stateMachine.\u003C\u003E1__state = -1;
stateMachine.\u003C\u003Et__builder.Start<Program.\u003CReadFileHierarchyAsync\u003Ed__2>(ref stateMachine);
return stateMachine.\u003C\u003Et__builder.Task;
}
private static void PrintWithThreadID(string text)
{
Console.WriteLine(text + " on thread: " + Thread.CurrentThread.ManagedThreadId.ToString());
}
public Program()
{
base.\u002Ector();
}
[CompilerGenerated]
[Serializable]
private sealed class \u003C\u003Ec
{
public static readonly Program.\u003C\u003Ec \u003C\u003E9;
public static Func<string> \u003C\u003E9__0_0;
static \u003C\u003Ec()
{
Program.\u003C\u003Ec.\u003C\u003E9 = new Program.\u003C\u003Ec();
}
public \u003C\u003Ec()
{
base.\u002Ector();
}
internal string \u003CMain\u003Eb__0_0()
{
return Program.ReadFileHierarchy("file1a");
}
}
[CompilerGenerated]
private sealed class \u003CReadFileHierarchyAsync\u003Ed__2 : IAsyncStateMachine
{
public int \u003C\u003E1__state;
public AsyncTaskMethodBuilder<string> \u003C\u003Et__builder;
public string fileOneName;
private Task<string> \u003CfileReadTask\u003E5__1;
private string \u003CfileTwoName\u003E5__2;
private string \u003CfileThreeName\u003E5__3;
private string \u003CfileThreeContents\u003E5__4;
private string \u003C\u003Es__5;
private string \u003C\u003Es__6;
private string \u003C\u003Es__7;
private TaskAwaiter<string> \u003C\u003Eu__1;
public \u003CReadFileHierarchyAsync\u003Ed__2()
{
base.\u002Ector();
}
void IAsyncStateMachine.MoveNext()
{
int num1 = this.\u003C\u003E1__state;
string fileThreeContents54;
try
{
TaskAwaiter<string> awaiter1;
int num2;
TaskAwaiter<string> awaiter2;
TaskAwaiter<string> awaiter3;
switch (num1)
{
case 0:
awaiter1 = this.\u003C\u003Eu__1;
this.\u003C\u003Eu__1 = new TaskAwaiter<string>();
this.\u003C\u003E1__state = num2 = -1;
break;
case 1:
awaiter2 = this.\u003C\u003Eu__1;
this.\u003C\u003Eu__1 = new TaskAwaiter<string>();
this.\u003C\u003E1__state = num2 = -1;
goto label_8;
case 2:
awaiter3 = this.\u003C\u003Eu__1;
this.\u003C\u003Eu__1 = new TaskAwaiter<string>();
this.\u003C\u003E1__state = num2 = -1;
goto label_11;
default:
Program.PrintWithThreadID("Starting Async Read");
this.fileOneName += ".txt";
this.\u003CfileReadTask\u003E5__1 = File.ReadAllTextAsync(this.fileOneName, new CancellationToken());
awaiter1 = this.\u003CfileReadTask\u003E5__1.GetAwaiter();
if (!awaiter1.IsCompleted)
{
this.\u003C\u003E1__state = num2 = 0;
this.\u003C\u003Eu__1 = awaiter1;
Program.\u003CReadFileHierarchyAsync\u003Ed__2 stateMachine = this;
this.\u003C\u003Et__builder.AwaitUnsafeOnCompleted<TaskAwaiter<string>, Program.\u003CReadFileHierarchyAsync\u003Ed__2>(ref awaiter1, ref stateMachine);
return;
}
break;
}
this.\u003C\u003Es__5 = awaiter1.GetResult();
this.\u003CfileTwoName\u003E5__2 = this.\u003C\u003Es__5;
this.\u003C\u003Es__5 = (string) null;
this.\u003CfileTwoName\u003E5__2 += ".txt";
Program.PrintWithThreadID("Got Second File Name Async: " + this.\u003CfileTwoName\u003E5__2);
awaiter2 = File.ReadAllTextAsync(this.\u003CfileTwoName\u003E5__2, new CancellationToken()).GetAwaiter();
if (!awaiter2.IsCompleted)
{
this.\u003C\u003E1__state = num2 = 1;
this.\u003C\u003Eu__1 = awaiter2;
Program.\u003CReadFileHierarchyAsync\u003Ed__2 stateMachine = this;
this.\u003C\u003Et__builder.AwaitUnsafeOnCompleted<TaskAwaiter<string>, Program.\u003CReadFileHierarchyAsync\u003Ed__2>(ref awaiter2, ref stateMachine);
return;
}
label_8:
this.\u003C\u003Es__6 = awaiter2.GetResult();
this.\u003CfileThreeName\u003E5__3 = this.\u003C\u003Es__6;
this.\u003C\u003Es__6 = (string) null;
this.\u003CfileThreeName\u003E5__3 += ".txt";
Program.PrintWithThreadID("Got Third File Name Async: " + this.\u003CfileThreeName\u003E5__3);
awaiter3 = File.ReadAllTextAsync(this.\u003CfileThreeName\u003E5__3, new CancellationToken()).GetAwaiter();
if (!awaiter3.IsCompleted)
{
this.\u003C\u003E1__state = num2 = 2;
this.\u003C\u003Eu__1 = awaiter3;
Program.\u003CReadFileHierarchyAsync\u003Ed__2 stateMachine = this;
this.\u003C\u003Et__builder.AwaitUnsafeOnCompleted<TaskAwaiter<string>, Program.\u003CReadFileHierarchyAsync\u003Ed__2>(ref awaiter3, ref stateMachine);
return;
}
label_11:
this.\u003C\u003Es__7 = awaiter3.GetResult();
this.\u003CfileThreeContents\u003E5__4 = this.\u003C\u003Es__7;
this.\u003C\u003Es__7 = (string) null;
fileThreeContents54 = this.\u003CfileThreeContents\u003E5__4;
}
catch (Exception ex)
{
this.\u003C\u003E1__state = -2;
this.\u003CfileReadTask\u003E5__1 = (Task<string>) null;
this.\u003CfileTwoName\u003E5__2 = (string) null;
this.\u003CfileThreeName\u003E5__3 = (string) null;
this.\u003CfileThreeContents\u003E5__4 = (string) null;
this.\u003C\u003Et__builder.SetException(ex);
return;
}
this.\u003C\u003E1__state = -2;
this.\u003CfileReadTask\u003E5__1 = (Task<string>) null;
this.\u003CfileTwoName\u003E5__2 = (string) null;
this.\u003CfileThreeName\u003E5__3 = (string) null;
this.\u003CfileThreeContents\u003E5__4 = (string) null;
this.\u003C\u003Et__builder.SetResult(fileThreeContents54);
}
[DebuggerHidden]
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment