Skip to content

Instantly share code, notes, and snippets.

@ping
Created November 30, 2018 03:10
Show Gist options
  • Save ping/29f0a42b4c20f79ffcadbca5e37113fd to your computer and use it in GitHub Desktop.
Save ping/29f0a42b4c20f79ffcadbca5e37113fd to your computer and use it in GitHub Desktop.
Async Test
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