CSharp · Console
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
// Warning CS1998: async method lacks 'await' operators,
// so it runs synchronously on the caller's thread.
static async Task<int> GetAsync(int x)
{
Thread.Sleep(1000);
return x;
}
public static async Task Main()
{
var sw = Stopwatch.StartNew();
var t1 = GetAsync(1); // blocks for 1s, returns an already completed task
var t2 = GetAsync(2); // blocks for another 1s
var c = await t1; // already done, no waiting
var d = await t2; // already done, no waiting
Console.WriteLine($"Β: {sw.ElapsedMilliseconds}");
}
}