0

Can someone explain if there is any difference between

 Task[] taskArray = new Task[]
 {
    Task.Factory.StartNew(() => GetData1()),
    Task.Factory.StartNew(() => GetData2())
 };

and just

Task.Factory.StartNew(() => GetData1());
Task.Factory.StartNew(() => GetData2());

Thank you!

4

3 回答 3

5

First sample is useful if you want to use the references to Tasks later, for ex

Task.WaitAll(taskArray);
于 2013-05-06T15:39:49.153 回答
4

The first stores both tasks in a collection that can be used later, to wait on them, add continuations, etc. The seconds creates the same tasks, but doesn't store them anywhere thus preventing any methods from being used on those tasks.

于 2013-05-06T15:40:16.573 回答
3

You don't need it unless you want to use some of the other features of the task parallel library. For Example many of the other taskfactory methods take an array of task like Task.Factory.ContinueWhenAll

于 2013-05-06T15:41:50.237 回答