Skip to content

Instantly share code, notes, and snippets.

@inkel
Created August 31, 2018 16:31
Show Gist options
  • Save inkel/525fc958556bc6a6a70675c0a4d3967f to your computer and use it in GitHub Desktop.
Save inkel/525fc958556bc6a6a70675c0a4d3967f to your computer and use it in GitHub Desktop.
Hello World!
A
Elapsed: 00:00:05.0107387
B
Elapsed: 00:00:02.5023560
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