【问题标题】:Parallel.ForEach is not executing the methodParallel.ForEach 没有执行该方法
【发布时间】:2014-09-09 14:30:47
【问题描述】:

我正在尝试并行化以下按预期工作的 For Each 循环。我从这个开始:

foreach (DataRow drGroup in dsGroups.Tables["Table"].Rows)
         ProduceInvoices(drGroup);

并将其更改为:

Parallel.ForEach<DataRow>((IEnumerable<DataRow>)dsGroups.Tables["Table"].Rows, ProduceInvoices)

然而ProduceInvoices 似乎不再被执行,尽管 dsGroup 包含行。

请你给我任何指示和/或在哪里看?

【问题讨论】:

  • 您应该抛出了某种无效的强制转换异常,您是否有一个空的catch 在某个地方吞下了错误?同时打开breaking on handled exceptions 也有助于追踪这类事情。
  • @ScottChamberlain - 这是一个写得很糟糕的遗留应用程序,所以很可能有奇怪的空catch,否则会发现这一点。下次我会尝试打破已处理的异常,谢谢。

标签: c# .net multithreading foreach task-parallel-library


【解决方案1】:

尝试使用AsEnumerable方法:

Parallel.ForEach<DataRow>(dsGroups.Tables["Table"].AsEnumerable(), ProduceInvoices);

Rows 属性返回一个 DataRowCollection,它没有实现 IEnumerable&lt;T&gt;

【讨论】:

  • 谢谢!我真的应该自己注意到这一点,而不是相信演员会重新锐化!
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多