开发者

How to initialize List with Task objects

开发者 https://www.devze.com 2023-02-02 07:07 出处:网络
开发者_运维问答How can i initialize a List with Task objects (TPL) using C# and .NET 4.0?Did you mean:

开发者_运维问答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

精彩评论

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