如下:
int i = 0;
List<string> strs = new List<string> { "111", "222", "333" };
while (i < 3)
{
var task = Task<List<string>>.Factory.StartNew(() =>
{
var list = new List<string>();
Thread.Sleep(2000);
list.Add(strs[i]);
i++;
return list;
});
tasks.Add(task);
}
代码 list.Add(strs[i]);会出错(i==3),那么,我要当用什么办法来将数据传入到task中呢?
直接上代码
var task = Task<List<string>>.Factory.StartNew((object value) =>
{
var list = new List<string>();
Thread.Sleep(2000);
list.Add(strs[i]);
i++;
return list;
}, "test");