Created
November 30, 2018 03:10
-
-
Save ping/29f0a42b4c20f79ffcadbca5e37113fd to your computer and use it in GitHub Desktop.
Async Test
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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
WriteLine("A: Main"); | |
var googleTask = CallGoogleAsync(); | |
WriteLine("B: Main"); | |
var microsoftTask = CallMicrosoftAsync(); | |
WriteLine("C: Main"); | |
googleTask.Wait(); | |
WriteLine("D: Main"); | |
microsoftTask.Wait(); | |
WriteLine("E: Main"); | |
} | |
private static void WriteLine(string text) { | |
Console.WriteLine(text); | |
} | |
private static async Task CallGoogleAsync() | |
{ | |
WriteLine("F: CallGoogleAsync"); | |
HttpClient client = new HttpClient(); | |
var stringTask = client.GetStringAsync("https://www.google.com"); | |
WriteLine("G: CallGoogleAsync"); | |
var content = await stringTask; | |
WriteLine("H: CallGoogleAsync"); | |
} | |
private static async Task CallMicrosoftAsync() | |
{ | |
WriteLine("I: CallMicrosoftAsync"); | |
HttpClient client = new HttpClient(); | |
var stringTask = client.GetStringAsync("https://docs.microsoft.com/en-us/"); | |
WriteLine("J: CallMicrosoftAsync"); | |
var content = await stringTask; | |
WriteLine("K: CallMicrosoftAsync"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment