Skip to content

Commit 1799246

Browse files
author
Anton Wieslander
committed
async await
1 parent 82ce79b commit 1799246

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29905.134
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwait", "AsyncAwait\AsyncAwait.csproj", "{7A02E0EA-683C-4C03-A721-AF94813F439C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7A02E0EA-683C-4C03-A721-AF94813F439C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7A02E0EA-683C-4C03-A721-AF94813F439C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7A02E0EA-683C-4C03-A721-AF94813F439C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7A02E0EA-683C-4C03-A721-AF94813F439C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1A45E28A-9071-475F-9F30-1BD2163588E6}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
5+
namespace AsyncAwait
6+
{
7+
class Program
8+
{
9+
static async Task Main(string[] args)
10+
{
11+
var client = new HttpClient();
12+
var task = await client.GetStringAsync("https://google.com");
13+
14+
int a = 0;
15+
for (int i = 0; i < 1_000_000; i++)
16+
{
17+
a = i + 1;
18+
}
19+
var task2 = await client.GetStringAsync("https://google.com");
20+
21+
Console.WriteLine("Hello World");
22+
}
23+
}
24+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Query Kind="Program">
2+
<Namespace>System.Threading.Tasks</Namespace>
3+
</Query>
4+
5+
async Task Main()
6+
{
7+
await MakeTeaAsync();
8+
}
9+
10+
11+
public async Task<string> MakeTeaAsync()
12+
{
13+
var boilingWater = BoilWaterAsync();
14+
15+
"take the cups out".Dump();
16+
17+
var a = 0;
18+
for (int i = 0; i < 100_000_000; i++){
19+
a += i;
20+
}
21+
22+
"put tea in cups".Dump();
23+
24+
var water = await boilingWater;
25+
26+
var tea = $"pour {water} in cups".Dump();
27+
28+
return tea;
29+
}
30+
31+
public async Task<string> BoilWaterAsync()
32+
{
33+
"Start the kettle".Dump();
34+
35+
"waiting for the kettle".Dump();
36+
await Task.Delay(300);
37+
38+
"kettle finished boiling".Dump();
39+
40+
return "water";
41+
}
42+
43+
public string MakeTea()
44+
{
45+
var water = BoilWater();
46+
47+
"take the cups out".Dump();
48+
49+
"put tea in cups".Dump();
50+
51+
var tea = $"pour {water} in cups".Dump();
52+
53+
return tea;
54+
}
55+
56+
public string BoilWater()
57+
{
58+
"Start the kettle".Dump();
59+
60+
"waiting for the kettle".Dump();
61+
Task.Delay(2000).GetAwaiter().GetResult();
62+
63+
"kettle finished boiling".Dump();
64+
65+
return "water";
66+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Query Kind="Program">
2+
<Namespace>System.Net.Http</Namespace>
3+
<Namespace>System.Threading.Tasks</Namespace>
4+
</Query>
5+
6+
async Task Main()
7+
{
8+
//part 1
9+
Thread.CurrentThread.ManagedThreadId.Dump("1");
10+
var client = new HttpClient();
11+
Thread.CurrentThread.ManagedThreadId.Dump("2");
12+
var task = client.GetStringAsync("http://google.com");
13+
Thread.CurrentThread.ManagedThreadId.Dump("3");
14+
15+
var a = 0;
16+
for (int i = 0; i < 1_000_000; i++)
17+
{
18+
a += i;
19+
}
20+
21+
Thread.CurrentThread.ManagedThreadId.Dump("4");
22+
var page = await task;
23+
// part 2
24+
Thread.CurrentThread.ManagedThreadId.Dump("5");
25+
}

0 commit comments

Comments
 (0)