How to initialize List with Task objects
开发者_运维问答How can i initialize a List with Task objects (TPL) using C# and .NET 4.0?
Did you mean:
// Create tasks
List<Task> tasks = new List<Task>()
{
new Task(() => Console.WriteLine("A")),
new Task(() => Console.WriteLine("B"))
};
// Start them later
tasks.ForEach(a => a.Start());
Or, if you want, start them at the moment of creation as Chad shown you (calling Task.Factory.StartNew(Action).
You mean:
var tasks = new List<Task>();
var task = Task.Factory.StartNew(() => {
//do work
});
tasks.Add(task);
精彩评论