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

查看:37
本文介绍了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?

谢谢

推荐答案

我认为 Jim G 的意思是:

I think what Jim G means is:

var lockObj = new object();

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

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

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