Skip to content

Instantly share code, notes, and snippets.

@Porges
Forked from TaliSoroker/async_await
Created July 27, 2017 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Porges/540acc54687681b9bb00c752486fc1cc to your computer and use it in GitHub Desktop.
Save Porges/540acc54687681b9bb00c752486fc1cc to your computer and use it in GitHub Desktop.
async/await
class Program
{
public static void Main()
{
Console.WriteLine("-- Main is on: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Hey David, How much is 98745 divided by 7?");
Task<int> david = ThinkAboutIt();
Console.WriteLine("While he thinks, lets chat about the weather for a bit.");
Console.WriteLine("Do you think it's going to rain tomorrow?");
Console.WriteLine("No, I think it should be sunny.");
Console.WriteLine("-- Main is still on: " + Thread.CurrentThread.ManagedThreadId);
var davidsAnswer = david.Result;
Console.WriteLine("-- Main is still on: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine($"David: {davidsAnswer}");
}
private static async Task<int> ThinkAboutIt()
{
Console.WriteLine("-- Starting thinking on: " + Thread.CurrentThread.ManagedThreadId);
await ReadTheManual();
Console.WriteLine("-- Thinking on: " + Thread.CurrentThread.ManagedThreadId);
return (98745 / 7);
}
private static async Task ReadTheManual()
{
Console.WriteLine("-- Starting reading a manual on: " + Thread.CurrentThread.ManagedThreadId);
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine("-- Done reading manual on: " + Thread.CurrentThread.ManagedThreadId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment