Created
August 31, 2018 16:31
-
-
Save inkel/525fc958556bc6a6a70675c0a4d3967f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello World! | |
A | |
Elapsed: 00:00:05.0107387 | |
B | |
Elapsed: 00:00:02.5023560 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
namespace AsyncTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
MainAsync().GetAwaiter().GetResult(); | |
Console.ReadLine(); | |
} | |
static async Task MainAsync() | |
{ | |
Console.WriteLine("A"); | |
await A(); | |
Console.WriteLine("B"); | |
await B(); | |
} | |
static async Task A() | |
{ | |
var s1 = Stopwatch.StartNew(); | |
s1.Start(); | |
var a = await Foo(); | |
var b = await Foo(); | |
s1.Stop(); | |
Console.WriteLine("Elapsed: {0}", s1.Elapsed); | |
} | |
static async Task B() | |
{ | |
var s1 = Stopwatch.StartNew(); | |
s1.Start(); | |
var aT = Foo(); | |
var bT = Foo(); | |
await Task.WhenAll(aT, bT); | |
s1.Stop(); | |
Console.WriteLine("Elapsed: {0}", s1.Elapsed); | |
} | |
static async Task<int> Foo() | |
{ | |
await Task.Delay(2500); | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment