Skip to content

Instantly share code, notes, and snippets.

@mvlabat
Created April 6, 2016 09:27
Show Gist options
  • Save mvlabat/f0a3028c54245b55620f8ecbc722f18b to your computer and use it in GitHub Desktop.
Save mvlabat/f0a3028c54245b55620f8ecbc722f18b to your computer and use it in GitHub Desktop.
Wait for all the continueWith. Tasks and Actions are used in the example.
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