我一直在Parallel.ForEach
对项目集合进行一些耗时的处理。该处理实际上是由外部命令行工具处理的,我无法更改它。然而,似乎Parallel.ForEach
会“卡在”集合中长期运行的项目上。我已经将问题提炼出来,并且可以表明Parallel.ForEach
,事实上,等待这个漫长的过程完成并且不允许任何其他人通过。我编写了一个控制台应用程序来演示该问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testParallel
{
class Program
{
static int inloop = 0;
static int completed = 0;
static void Main(string[] args)
{
// initialize an array integers to hold the wait duration (in milliseconds)
var items = Enumerable.Repeat(10, 1000).ToArray();
// set one of the items to 10 seconds
items[50] = 10000;
// Initialize our line for reporting status
Console.Write(0.ToString("000") + " Threads, " + …
Run Code Online (Sandbox Code Playgroud) c# parallel-processing blocking task-parallel-library parallel.foreach