如何使用一个固定参数和集合中的另一个参数编写 Parallel.ForEach?

发布于 2025-01-08 17:31:32 字数 325 浏览 3 评论 0原文

我有一个像这样的 foreach 方法:

public void Initialize(ClassB fixed)
{
    foreach (ClassA item in itemCollection)
    {
        this.InitializeStock(fixed, item);
    }
}        

我想将 Parallel.ForEach 与此方法一起使用,但不确定如何做到这一点。我无法将固定参数设置为类属性,因为已经从另一个 Parallel.ForEach 调用了 Initialize 方法。

提前致谢。

I have a foreach method like this one:

public void Initialize(ClassB fixed)
{
    foreach (ClassA item in itemCollection)
    {
        this.InitializeStock(fixed, item);
    }
}        

I would like to use a Parallel.ForEach with this one but not sure on how to do it. I cannot set the fixed parameter as a class attribute as the Initialize method is already called from another Parallel.ForEach.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2025-01-15 17:31:32

目前还不清楚问题是什么。这应该没问题:

public void Initialize(ClassB fixed)
{
    Parallel.ForEach(itemCollection, item =>
    {
        this.InitializeStock(fixed, item);
    });
}

fixed 变量将被 lambda 表达式捕获,以便在调用 InitializeStock 时使用它。

编辑:如果你真的不想要 lambda 表达式:

private class ClassBHolder
{
    private readonly ClassB fixed;
    // Foo is the class which has the Initialize method
    private readonly Foo container;

    internal ClassBHolder(ClassB fixed, Foo container)
    {
        this.fixed = fixed;
        this.container = container;
    }

    internal void Execute(ClassA item)
    {
        container.InitializeStock(fixed, item);
    }
}

public void Initialize(ClassB fixed)
{
    ClassBHolder holder = new ClassBHolder(fixed, this);
    Parallel.ForEach(itemCollection, holder.Execute);
}

...但是实际上,你更愿意读哪一个?

It's not clear what the problem is. This should be fine:

public void Initialize(ClassB fixed)
{
    Parallel.ForEach(itemCollection, item =>
    {
        this.InitializeStock(fixed, item);
    });
}

The fixed variable will be captured by the lambda expression so that it can be used when calling InitializeStock.

EDIT: If you really don't want lambda expressions:

private class ClassBHolder
{
    private readonly ClassB fixed;
    // Foo is the class which has the Initialize method
    private readonly Foo container;

    internal ClassBHolder(ClassB fixed, Foo container)
    {
        this.fixed = fixed;
        this.container = container;
    }

    internal void Execute(ClassA item)
    {
        container.InitializeStock(fixed, item);
    }
}

public void Initialize(ClassB fixed)
{
    ClassBHolder holder = new ClassBHolder(fixed, this);
    Parallel.ForEach(itemCollection, holder.Execute);
}

... but really, which would you rather read?

暮年慕年 2025-01-15 17:31:32

在这里试试这个:

public void Initialize(ClassB fixed)
{
    Parallel.ForEach(itemCollection, new ParallelOptions() { MaxDegreeOfParallelism = 100 },
                (item, i, j) =>
                     {

                         this.InitializeStock(fixed, item);

                     });
}  

here try this:

public void Initialize(ClassB fixed)
{
    Parallel.ForEach(itemCollection, new ParallelOptions() { MaxDegreeOfParallelism = 100 },
                (item, i, j) =>
                     {

                         this.InitializeStock(fixed, item);

                     });
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文