Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View ABitMoreAdvancedYieldAwait.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static void Main(string[] args)
{
Task.WaitAll(Execute(new int[] { 1, 2, 3, 4 }));
Console.ReadLine();
}
 
static async Task Execute(IEnumerable<int> values)
{
foreach (var task in AsyncEnumerable(values))
{
if (task is Task<string>)
{
var value = await ((Task<string>)task);
Console.WriteLine("Got value {0}", value);
}
else
{
await task;
}
}
}
 
static IEnumerable<Task> AsyncEnumerable(IEnumerable<int> values)
{
yield return WriteLineNoResult("Starting...");
foreach(var item in values)
{
Console.WriteLine("Start of loop body {0}", item);
yield return WriteLine(item.ToString());
Console.WriteLine("End of loop body {0}", item);
}
yield return WriteLineNoResult("Ending...");
}
 
static async Task WriteLineNoResult(string value)
{
Console.WriteLine(value);
await TaskEx.Delay(1000); // Such an expensive call.
}
 
static async Task<string> WriteLine(string value)
{
Console.WriteLine(value);
await TaskEx.Delay(1000); // Such an expensive call.
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.