我想用List做第一种方法处理数组的方法。情况是不知道前面有多少个任务。我不想在开始任务之前填写列表,因为要花一些时间来填写列表。public void TaskArray(){ ConcurrentBag<int> cb = new ConcurrentBag<int>(); Task[] taskArray = new Task[10]; for (int i = 0; i < 100; i++) { taskArray[i] = Task.Factory.StartNew((Object obj) => { Console.WriteLine("Task #{0} created at {1} on thread #{2}.", i, Thread.CurrentThread.ManagedThreadId); }, i); } Task.WaitAll(taskArray);}这有语法错误。无法从system.generic.task.list转换为system.threading.task.taskpublic void TaskList(){ ConcurrentBag<int> cb = new ConcurrentBag<int>(); List<Task> taskArray = new List<Task>; for (int i = 0; i < 100; i++) { taskArray.Add(Task.Factory.StartNew((Object obj) => { cb.Add(i); Console.WriteLine("Task #{0} created at {1} on thread #{2}.", i, Thread.CurrentThread.ManagedThreadId); }, i)); } Task.WaitAll(taskArray);}
- 1 回答
- 0 关注
- 131 浏览
添加回答
举报
0/150
提交
取消