开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜