Created
April 6, 2016 09:27
-
-
Save vladbat00/f0a3028c54245b55620f8ecbc722f18b to your computer and use it in GitHub Desktop.
Wait for all the continueWith. Tasks and Actions are used in the example.
This file contains hidden or 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 private Func<int, int, int> count = (count, price) => | |
{ | |
return count * price; | |
}; | |
private void calcButton_Click(object sender, EventArgs e) | |
{ | |
int firstCount, secondCount, thirdCount; | |
int price = 0; | |
/** | |
* ... initializing firstCount, secondCount, thirdCount. | |
*/ | |
Task<int>[] tasks = new Task<int>[] { | |
Task<int>.Factory.StartNew(() => count(firstCount, 60)), | |
Task<int>.Factory.StartNew(() => count(secondCount, 80)), | |
Task<int>.Factory.StartNew(() => count(thirdCount, 85)) | |
}; | |
Task[] addTasks = new Task[tasks.Length]; | |
for (int i = 0; i < tasks.Length; ++i) | |
{ | |
addTasks[i] = tasks[i].ContinueWith(t => price += t.Result); | |
} | |
Task.WaitAll(addTasks); | |
priceLabel.Text = price.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment