-
-
Save Porges/540acc54687681b9bb00c752486fc1cc to your computer and use it in GitHub Desktop.
async/await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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