using System.Net;
Console.WriteLine("start!");
await HttpGetAsync("http://localhost:5203/sleep/5"); // Mock sleep api that will return after 5 seconds.
await HttpGetAsync("http://localhost:5203/sleep/3"); // ... after 3 seconds.
int i = 0;
while (i < 20)
{
Console.WriteLine(i++);
Thread.Sleep(100);
}
Console.WriteLine("end!");
Console.ReadKey();
static async Task<HttpStatusCode> HttpGetAsync(string url)
{
using HttpClient client = new();
HttpResponseMessage result = await client.GetAsync(url);
Console.WriteLine(await result.Content.ReadAsStringAsync());
return result.StatusCode;
}
I got this:
Isn't Task.Run() run in background thread? My expected result is:
0 1 2 ... 20
After 3 second, I'm awake!
After 5 second, I'm awake!