How to pass a state variable into the parallel loops

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
i am looking for sample example where iterate a list data with parallel.foreach where i can pass a state variable into the parallel loops. how to pass rowNumber state from main parallel loop to inner parallel loop?

Parallel.ForEach(OrderWiseLineItem, data =>
{
// string Li = data1.LineItem;
string section = data.Section;
string Li = data.Lineitem;

if (!String.IsNullOrEmpty(Li) && !String.IsNullOrEmpty(section))
{
Parallel.ForEach(DistinctBroker, broker =>
{
// for broker row .... weightage 1 (no color)

Interlocked.Increment(ref rowNumber);
brokerRowWeightageRowNumber = new WeightageRowNumber();
brokerRowWeightageRowNumber.Section = section;
brokerRowWeightageRowNumber.Lineitem = Li;
brokerRowWeightageRowNumber.Broker = broker;
brokerRowWeightageRowNumber.RowNumber = rowNumber;
brokerRowWeightageRowNumber.Weightage = (int)RowWeightage.BrokerRow;

lock (_lock)
{
WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
}
}

Interlocked.Increment(ref rowNumber);
ConsensusRowWeightageRowNumber = new WeightageRowNumber();
ConsensusRowWeightageRowNumber.Section = section;
ConsensusRowWeightageRowNumber.Lineitem = Li;
ConsensusRowWeightageRowNumber.Broker = "";
ConsensusRowWeightageRowNumber.RowNumber = rowNumber;
ConsensusRowWeightageRowNumber.Weightage = (int)RowWeightage.ConsenSusRow;

lock (_lock)
{
WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
}


if (qcTrueDistin.Any(x => x.TabName.Equals(section) && x.StandardLineItem.Equals(Li)))
{
// for QC Check row .... weightage 3, if any (yellow color)
Parallel.ForEach(DistinctBroker, broker =>
{

Interlocked.Increment(ref rowNumber);
QcRowWeightageRowNumber = new WeightageRowNumber();

QcRowWeightageRowNumber.Section = section;
QcRowWeightageRowNumber.Lineitem = Li;
QcRowWeightageRowNumber.Broker = broker;
QcRowWeightageRowNumber.RowNumber = rowNumber;
QcRowWeightageRowNumber.Weightage = (int)RowWeightage.QcRow;
lock (_lock)
{
WeightageRowNumberall.Add(QcRowWeightageRowNumber);
}

}
}
}

}

Continue reading...
 
Back
Top