It is not clear what the problem is. This should be good:
public void Initialize(ClassB fixed)
{
Parallel.ForEach(itemCollection, item =>
{
this.InitializeStock(fixed, item);
});
}
The variable fixedwill be written using the lambda expression so that it can be used in the call InitializeStock.
EDIT: If you really don't need lambda expressions:
private class ClassBHolder
{
private readonly ClassB fixed;
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, what would you rather read?
source
share