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
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