[英]Parallel.ForEach is not working as expected C#
I am replacing ForEach to Parallel.ForEach. 我正在将ForEach替换为Parallel.ForEach。 ForEach is working fine and gives result as expected where as Parallel.ForEach is giving unexpected result.
ForEach工作正常,并给出预期的结果,与Parallel.ForEach产生意外结果。
string[] ids= { };
string input="AA;AA;111;T#BB;BB;222;T#CC;CC;333;F";
string typeId, type, value;
typeId= type=value=string.Empty;
bool isValid = false;
if (input.Contains("#"))
{ ids = input.Split('#'); }
else
{ ids = new string[] { input };}
//using ForEach ,it's working fine,datas are inserted in DB table properly.
foreach (var id in ids)
{
if (id.Contains(";"))
{
var IdInfo = id.Split(';');
if (IdInfo[0] != null)
{
typeId = Info[0];
}
if (IdInfo.Length >= 2)
{ type = IdInfo[1]; }
if (IdInfo.Length >= 3)
{ value = IdInfo[2]; }
if (IdInfo.Length >= 4)
{ isValid = IdInfo[3]=="T" ? true:false; } // T=true
}
else
{
return;
}
DBInsertMethod("someuniqueId", typeId, type, value, isValid);
}
I have replaced same piece of code with Parrallel.ForEach 我已经用Parrallel.ForEach替换了同一段代码
Parallel.ForEach ( ids,(id)=>
{
if (id.Contains(";"))
{
var IdInfo = id.Split(';');
if (IdInfo[0] != null)
{
typeId = Info[0];
}
if (IdInfo.Length >= 2)
{ type = IdInfo[1]; }
if (IdInfo.Length >= 3)
{ value = IdInfo[2]; }
if (IdInfo.Length >= 4)
{ isValid = IdInfo[3]=="T" ? true:false; } // T=true
}
else
{
return;
}
DBInsertMethod("someuniqueId", typeId, type, value, isValid);
});
Now same row is inserted three times in table.Any suggestion/advise is appreciated. 现在同一行在表中插入了三遍。任何建议/建议都值得赞赏。
So you have these variables: 因此,您具有以下变量:
string typeId, type, value;
bool isValid;
If I run this foreach, at any one time typeId is being accessed once. 如果我运行此foreach,则任何时候typeId都会被访问一次。
foreach (var item in list)
{
typeId = item.id;
}
Now if I make it parallel, at any one time typeId is being accessed by N threads. 现在,如果我将其并行化,那么在任何一次N线程都将访问typeId。 N threads that can update it at any time.
N个线程,可以在任何时间更新。
Parallel.ForEach(list, item =>
{
typeId = item.id;
});
Move your temporary variables into the ForEach so that they're scoped to the anonymous function: 将您的临时变量移至ForEach中,以便将它们的范围限定为匿名函数:
Parallel.ForEach(list, item =>
{
string typeId, type, value;
bool isValid;
typeId = item.id;
});
Actually even if Paralle.Foreach created multiple threads to handle parallel processing, some variables are declared outside of it scope and thus they are unique. 实际上,即使Paralle.Foreach创建了多个线程来处理并行处理,某些变量也会在其作用域之外声明,因此它们是唯一的。 Try to declare them inside the lambda:
尝试在lambda中声明它们:
Parallel.ForEach ( ids,(id)=>
{
string typeId, type, value;
typeId= type=value=string.Empty;
bool isValid=false;
if (id.Contains(";"))
{
var IdInfo = id.Split(';');
if (IdInfo[0] != null)
{
typeId = Info[0];
}
if (IdInfo.Length >= 2)
{ type = IdInfo[1]; }
if (IdInfo.Length >= 3)
{ value = IdInfo[2]; }
if (IdInfo.Length >= 4)
{ isValid = IdInfo[3]=="T"; } // T=true
}
else
{
return;
}
DBInsertMethod("someuniqueId", typeId, type, value, isValid);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.