-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync-syncProgram.cs
More file actions
64 lines (59 loc) · 1.77 KB
/
Async-syncProgram.cs
File metadata and controls
64 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
const string url = @"https://hraverkar.netlify.com";
static void Main(string[] args)
{
DoSynchronousWork();
var t = DoSomethingAsynchronousWork();
DoAsynchronousWorkAfterWait();
t.Wait();
}
private static void DoAsynchronousWorkAfterWait()
{
Console.WriteLine("7. While waiting for the async task to finish, we can do some unrelated work...");
for (var i = 0; i <= 5; i++)
{
for (var j = i; j <= 5; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
private static async Task DoSomethingAsynchronousWork()
{
Console.WriteLine("2. Got some data from asynchronous class");
var test = await getAwaitClassExecution();
Console.WriteLine("4. The awaited task has completed. Let's get the content length...");
Console.WriteLine("5. The length of http Get for " + url);
Console.WriteLine($"6. {test.Length} character");
}
private static async Task<string> getAwaitClassExecution()
{
using var httpClient = new HttpClient();
Console.WriteLine("3. Awaiting the result of getAwaitClassExecution of Http Client...");
try
{
var result = await httpClient.GetStringAsync(url);
string checkResult = result.ToString();
httpClient.Dispose();
return checkResult;
}
catch (Exception ex)
{
string checkResult = "Error " + ex.ToString();
httpClient.Dispose();
return checkResult;
}
}
private static void DoSynchronousWork()
{
Console.WriteLine("1. Doing something synchronous work progress will wait for finish.");
}
}
}