-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTaskWithoutAsyncProgram.cs
More file actions
70 lines (63 loc) · 2.12 KB
/
Copy pathTaskWithoutAsyncProgram.cs
File metadata and controls
70 lines (63 loc) · 2.12 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
65
66
67
68
69
70
namespace Otus.AsyncAwait
{
public class TaskWithoutAsync
{
public async Task Execute1Async()
{
Task task;
//task = GetWithKeywordsAsync("https://google.com");
//await task;
task = GetElidingKeywordsAsync("https://google.com");
await task;
}
public async Task Execute2Async()
{
HttpResponseMessage httpResponseMessage;
//httpResponseMessage = await GetWithKeywordsAsync("https://google.com");
httpResponseMessage = await GetElidingKeywordsAsync("https://google.com");
Console.WriteLine(httpResponseMessage.Version);
}
public async Task Execute3Async()
{
Console.WriteLine($"Main flow started.");
var task = Task.Run(() =>
{
Console.WriteLine($"Inside task.");
})
.ContinueWith(async t =>
{
var res= GetElidingKeywordsAsync("https://google.com");
await res;
});
await await task; //Task возвращает Task от ContinueWith, поэтому ждем дважды
Console.WriteLine("Main flow finished");
}
private Task<bool> WaitAndWriteStart()
{
return WaitAndWriteStartInternal();
}
private Task<bool> WaitAndWriteStartInternal()
{
throw new Exception("Ex!!");
Task.Delay(2000);
Console.WriteLine("finish!");
return Task.FromResult(true);
}
private async Task<HttpResponseMessage> GetWithKeywordsAsync(string url)
{
//throw new Exception("Ex!!");
using (var client = new HttpClient())
{
return await client.GetAsync(url);
}
}
private Task<HttpResponseMessage> GetElidingKeywordsAsync(string url)
{
//throw new Exception("Ex!!");
using (var client = new HttpClient())
{
return client.GetAsync(url);
}
}
}
}