HOWTO:Parallel.Foreach执行多个进程,每个进程之后运行新的处理(但一次一个)? [英] Howto: Parallel.Foreach executes many processes, after each process run a new process (but one at a time)?

查看:288
本文介绍了HOWTO:Parallel.Foreach执行多个进程,每个进程之后运行新的处理(但一次一个)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定有人知道这一点,我将是答案非常感谢。我不知道很多关于代表和非同步之类的 - 所以请给我的,我怎么能实现一个普通的例子。

I'm sure someone knows this and I will be very thankful for the answer. I don't know much about delegates and asynch and the like - so please give me a general example of how I could implement.

我有一个工作流程,
口可以使用Parallel.Foreach同时执行许多不同的文件的方法(甜,研磨该处理器) -
该方法结束然而后,我需要运行的另一种方法(它产生比上一报告工艺),
和第二种方法不能并行运行。

I have a workflow where I can use Parallel.Foreach to execute a method for many different files at the same time (sweet, grind that processor) - however after that method ends I need to run another method (it generates a report on the previous process), and this second method cannot be run in parallel.

我不希望等待所有文件在Parallel.ForEach完成生成报告之前(这是没有必要的)。但是,如果我开始报告生成方法,第一个方法结束,然后我遇到的问题。是否有某种队列或什么的?有一定有做这件事的一些漂亮的方式,不是吗?

I don't want to wait for all the files in the Parallel.ForEach to finish before generating reports (that's not necessary). But if I start the report generation method as the first method ends then I run into problems. Is there some kind of queue or something? There's gotta be some pretty way of doing it, right?

感谢

推荐答案

我想吉姆G表示为:

var lockObj = new object();

Parallel.Foreach(files, file => 
{
    // Processing file
    lock(lockObj)
    {
        // Generate report.
    }
});

这篇关于HOWTO:Parallel.Foreach执行多个进程,每个进程之后运行新的处理(但一次一个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆