Task 带参数和返回值的多线程调用
static void Main(string[] args) { // 无参数异步 var tasks = new List<Task>(); for (int i = 0; i < 5000; i++) { tasks.Add(Task.Run(() => { Console.WriteLine(i); })); } Task.WaitAll(tasks.ToArray()); // 带参数异步 var taskList = new List<Task<List<int>>>(); for (int i = 0; i < 5000; i++) { taskList.Add(Task.Factory.StartNew(state => { var value= state as int?; return new List<int> { value ?? 0 }; },i)); } Task.WaitAll(taskList.ToArray()); List<int> list = new List<int>(); foreach (var item in taskList) { list.AddRange(item.GetAwaiter().GetResult()); } Console.ReadKey(); }