Skip to content

Instantly share code, notes, and snippets.

@binki
Created June 14, 2016 15:04
Show Gist options
  • Save binki/fb41b1e3166f83fc1c5ad06a4a42eab4 to your computer and use it in GitHub Desktop.
Save binki/fb41b1e3166f83fc1c5ad06a4a42eab4 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
namespace ArgumentListAwait
{
class Program
{
// Too bad to call a function with multiple task results you have to
// store the intermediate tasks in locals because of the order of
// evaluation of argument lists.
static void Main(string[] args)
{
// Slow
//
// +A
// -A
// +B
// -B
// +C
// -C
// Called
Task.Run(
async () =>
{
await Argumentative(
await LoudDelay("A"),
await LoudDelay("B"),
await LoudDelay("C"));
}).Wait();
// Fast:
//
// +A
// +B
// +C
// -C
// -A
// -B
// Called
Task.Run(
async () =>
{
var a = LoudDelay("A");
var b = LoudDelay("B");
var c = LoudDelay("C");
await Argumentative(
await a,
await b,
await c);
}).Wait();
}
static Task Argumentative(
params object[] p)
{
Console.WriteLine("Called");
return Task.CompletedTask;
}
static async Task<object> LoudDelay(
string id)
{
Console.WriteLine($"+{id}");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine($"-{id}");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment