如何使用c#.net 4.0在最大已定义的并行线程中运行任务 [英] How to run a task in max defined parallel threads using c#.net 4.0

查看:59
本文介绍了如何使用c#.net 4.0在最大已定义的并行线程中运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个方法要在10个不同的并行线程中运行,所有方法都是独立的,彼此之间没有依赖性,我的问题是,如果我有100个要处理的项目,并且想一次处理10个,然后一次运行10次.我已经在使用 Parallel.ForEach 的地方创建了一个示例代码,但是我需要设置什么,因此它应该一次运行10个线程,并且假设任何正在运行的任务已经完成,那么它应该自动换一个新线程,因此所有10个线程应该忙碌,直到所有项目都没有完成为止.

I have 1 method that I want to run in 10 different parallel threads, all will be independent there are no dependency on each other, my problem is that if I have 100 items to process and would like to process 10 at a time then how do in run 10 at a time. I have created a sample code where I am using Parallel.ForEach but what do I need to set so it should run 10 threads at a time and suppose any of the the running task have completed then it should automatically take new one, so all 10 threads should be busy until and unless all items are not completed.

    private void StartAccuracyCalculator()
    {
        List<MaterialComposition> lstMaterialComposition = DataHelper.GetMaterialComposition();

        Parallel.ForEach(lstMaterialComposition, composition =>
        {
            try
            {
                CalculateAccuracy(composition);
            }
            catch (Exception ex)
            {
                //LogException(ex)
            }
        });

    }


    private void CalculateAccuracy(MaterialComposition composition)
    {
        /// actual process to perform 
    }

假设 lstMaterialComposition 从数据库中获得100条记录,所以在 Parallel.ForEach 中,我一次只能运行10个项目,而10个项目中的任何一个都已完成,因此下一个项目应该从 lstMaterialComposition 开始.

suppose lstMaterialComposition get the 100 records from DB so in Parallel.ForEach I want to run only 10 item at a time and any of 10 have completed so a next item from lstMaterialComposition should start.

请建议是否可以通过 Parallel.ForEach 进行操作,或者是否还有其他选择可以实现?

please suggest is it possible though Parallel.ForEach or is there other option to do it?

推荐答案

您可以使用

You can use ParallelOptions.MaxDegreeOfParallelism Property to limit the number of tasks.

MaxDegreeOfParallelism MaxDegreeOfParallelism属性影响通过此ParallelOptions实例传递的Parallel方法调用运行的并发操作的数量.正属性值将并发操作数限制为设置值.如果为-1,则并发运行的操作数没有限制.

MaxDegreeOfParallelism The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance. A positive property value limits the number of concurrent operations to the set value. If it is -1, there is no limit on the number of concurrently running operations.

Parallel.ForEach(
    lstMaterialComposition,
    new ParallelOptions { MaxDegreeOfParallelism = 10 },
    composition => { 
            try
            {
                CalculateAccuracy(composition);
            }
            catch (Exception ex)
            {
                //LogException(ex)
            }
     }
);

这篇关于如何使用c#.net 4.0在最大已定义的并行线程中运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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