Instantly share code, notes, and snippets.

Embed
What would you like to do?
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