最大并行线程foreach循环

时间:2013-10-22 07:48:11

标签: c# asp.net multithreading asynchronous parallel.foreach

如何为此循环添加最大并行线程:

Parallel.ForEach(DataRow drValue in dtValues.Rows)
{
}

这不起作用:

Parallel.ForEach(DataRow drValue in dtValues.Rows, new ParallelOptions {MaxDegreeOfParallelism = 4})
{
}

1 个答案:

答案 0 :(得分:2)

尝试使用lambda:

Parallel.ForEach(dtValues.Rows.AsEnumerable(), new ParallelOptions { MaxDegreeOfParallelism = 4 }, drValue =>
{
    //logic goes here
});

要使用此方法,您必须在项目中包含System.Data.DataSetExtensions.dll。请参阅here

相关问题