Skip to content

Instantly share code, notes, and snippets.

@jnnnnn
Last active January 5, 2017 04:02
Show Gist options
  • Save jnnnnn/677a9fb9440d13c0d704dcd98e4deaae to your computer and use it in GitHub Desktop.
Save jnnnnn/677a9fb9440d13c0d704dcd98e4deaae to your computer and use it in GitHub Desktop.
private void NormalMethod()
{
Task.Run(() => TestAsync()); // runs the task on the threadpool
}
private async void TestAsync()
{
Console.WriteLine("01. Starting test async");
await T1(); // T1 will return a Task when (if) it hits its first blocker. await will then pause execution of this method until the Task completes.
Console.WriteLine("03. Continuing test async"); // We then resume execution of this method with the same context (locals, callstack, etc.) as before (but maybe on a different physical thread)
await T2();
Console.WriteLine("05. Finished synched async");
T3(); // starts executing T3. When T3 gets to its first await, it will return here (and T3 will keep executing on another thread when the await finishes)
Console.WriteLine("06. Continuing test async");
T4(); // You can only "await" Task objects, nothing else, so async void methods are like orphaned threads in that you can't "join" (wait for them to finish).
Console.WriteLine("07. Finished unsynched async");
}
private async Task T1()
{
await Task.Delay(100);
Console.WriteLine("02. T1");
}
private async Task T2()
{
await Task.Delay(100);
Console.WriteLine("04. T2");
}
private async void T3()
{
await Task.Delay(200);
Console.WriteLine("09. T3");
}
private async void T4()
{
await Task.Delay(100);
Console.WriteLine("08. T4");
}
/*
output:
01. Starting test async
02. T1
03. Continuing test async
04. T2
05. Finished synched async
06. Continuing test async
07. Finished synched async
08. T4
09. T3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment