Skip to content

Instantly share code, notes, and snippets.

@brenoferreira
Created November 7, 2011 01:02
Show Gist options
  • Save brenoferreira/1343940 to your computer and use it in GitHub Desktop.
Save brenoferreira/1343940 to your computer and use it in GitHub Desktop.
Async+Await under the covers
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
[CompilerGenerated]
[StructLayout(LayoutKind.Auto)]
private struct <DownloadBingHomePageHTMLAsync>d__3 : <>t__IStateMachine
{
private int <>1__state;
public AsyncTaskMethodBuilder<string> <>t__builder;
public Action <>t__MoveNextDelegate;
public WebClient <webClient>5__4;
public string <html>5__5;
private object <>t__stack;
private object <>t__awaiter;
public void MoveNext()
{
string result;
try
{
int num = this.<>1__state;
TaskAwaiter<string> taskAwaiter;
if (num != 1) //if execution of async method is not finished (State == 1 means that the execution is running)
{
if (this.<>1__state == -1)
{
return;
}
this.<webClient>5__4 = new WebClient();
taskAwaiter = this.<webClient>5__4.DownloadStringTaskAsync("http://www.bing.com").GetAwaiter(); //start the async operation, and get the TaskAwaiter
if (!taskAwaiter.IsCompleted) //if the async operation is not completed
{
this.<>1__state = 1; //set the state to "Running"
TaskAwaiter<string>[] array;
(array = new TaskAwaiter<string>[1])[0] = taskAwaiter;
this.<>t__awaiter = array;
Action action;
if ((action = this.<>t__MoveNextDelegate) == null)
{
Task<string> arg_9A_0 = this.<>t__builder.Task;
action = new Action(this.MoveNext); //define a delegate to the method itself.
((<>t__IStateMachine)action.Target).<>t__SetMoveNextDelegate(action);
}
array[0].OnCompleted(action); //use that delegate (which points to the method itself) as a method which will be called when the async operation completes
return;
}
}
else //if the async is complete
{
taskAwaiter = ((TaskAwaiter<string>[])this.<>t__awaiter)[0]; //get the TaskAwaiter
this.<>t__awaiter = null;
this.<>1__state = 0; //set the state to "Initial"
}
string arg_110_0 = taskAwaiter.GetResult(); //get the result of the async operation from the TaskAwaiter
taskAwaiter = default(TaskAwaiter<string>);
string text = arg_110_0; //continue with the execution of the code that was defined after the "await" keyword (in this case, return the html as a String)
this.<html>5__5 = text;
result = this.<html>5__5;
}
catch (Exception exception)
{
this.<>1__state = -1;
this.<>t__builder.SetException(exception);
return;
}
this.<>1__state = -1; //set the result to "Complete"
this.<>t__builder.SetResult(result);
}
[DebuggerHidden]
void <>t__IStateMachine.<>t__SetMoveNextDelegate(Action param0)
{
this.<>t__MoveNextDelegate = param0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment