I have a problem implementing Parallel.ForEach in DTable. Following is the sample code that I am implementing:
Parallel.ForEach(dTable4.AsEnumerable(), row4 =>
{
string getCondition = row4[1].ToString();
getCondition = EvaluateCondExpression(getCondition); //Evaluates CM for value (CM1==2 && CM2<5);
Expression e = new Expression(getCondition); //getCondition = (2==2 && 2<5)
var evalutateCondition = e.Evaluate();
if (Convert.ToBoolean(evalutateCondition))
{
//Write string to TextBox
}
}
When I run this, the threads are not efficiently managed and it takes far too time than foreach loop. EvaluateCondExpression function returns value after checking the user provided parameter from GUI combobox and numericUpDown
private string EvaluateCondExpression(string getCondition)
{
string[] splitgetCondition1 = SeprateCharacter(getCondition);
foreach (string oneCondition in splitgetCondition1)
{
string conditionValue = oneCondition;
if (oneCondition.Contains("CM"))
{
conditionValue = RemoveMultipleChar(conditionValue.Replace('!', ' ').Trim());
string getInputNumber = getTableOneInputNo(conditionValue, dTable1);
string tableOneValue = checkComboxValue(m_comboBox, getInputNumber);
getCondition = getCondition.Replace(conditionValue, tableOneValue);
}
}
return getCondition;
}
The serial ForEach computation is taking too much time so I wanted to apply Parallel.ForEach iteration but unfortunately it is not working. Can anyone suggest me how to maximize the performance and what I am doing wrong in Parallel.ForEach.