Created
June 3, 2018 16:43
-
-
Save NMZivkovic/2a31cc18d4b0f358417524bb5611fe5a to your computer and use it in GitHub Desktop.
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
async Task RunWorkflow() | |
{ | |
var stopwatch = Stopwatch.StartNew(); | |
// Task 1 takes 2 seconds to be done. | |
var task1 = Task.Delay(2000) | |
.ContinueWith(task => Completed("Task 1", stopwatch.Elapsed)); | |
// Task 2 takes 3 seconds to be done. | |
var task2 = Task.Delay(3000) | |
.ContinueWith(task => Completed("Task 2", stopwatch.Elapsed)); | |
// Task 3 takes 1 second to be done. | |
var task3 = Task.Delay(1000) | |
.ContinueWith(task => Completed("Task 3", stopwatch.Elapsed)); | |
var tasks = new List<Task>() { task1, task2, task3 }; | |
while (tasks.Count > 0) | |
{ | |
var finishedTask = await Task.WhenAny(tasks); | |
tasks.Remove(finishedTask); | |
Console.WriteLine($"Additional processing after finished Task! {tasks.Count} more tasks remaining!"); | |
} | |
// Print the final result. | |
Completed("Workflow: ", stopwatch.Elapsed); | |
stopwatch.Stop(); | |
} | |
void Completed(string name, TimeSpan time) | |
{ | |
Console.WriteLine($"{name} : {time}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment