如何使用一个固定参数和集合中的另一个参数编写 Parallel.ForEach?
我有一个像这样的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前还不清楚问题是什么。这应该没问题:
fixed
变量将被 lambda 表达式捕获,以便在调用InitializeStock
时使用它。编辑:如果你真的不想要 lambda 表达式:
...但是实际上,你更愿意读哪一个?
It's not clear what the problem is. This should be fine:
The
fixed
variable will be captured by the lambda expression so that it can be used when callingInitializeStock
.EDIT: If you really don't want lambda expressions:
... but really, which would you rather read?
在这里试试这个:
here try this: