How to do proper Parallel.ForEach, locking and progress reporting

后端 未结 6 1263
小鲜肉
小鲜肉 2020-12-17 18:48

I\'m trying to implement the Parallel.ForEach pattern and track progress, but I\'m missing something regarding locking. The following example counts to 1000 whe

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 19:13

    Remove lock statements from properties and modify Main body:

     object sync = new object();
            Parallel.ForEach(ids, new ParallelOptions {MaxDegreeOfParallelism = threadCount},
                             id =>
                                 {
                                     lock(sync)
                                     progress.CurrentCount++;
                                 });
    

提交回复
热议问题