繁体   English   中英

Parallel.ForEach无法按预期的方式运行C#

[英]Parallel.ForEach is not working as expected C#

我正在将ForEach替换为Parallel.ForEach。 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);
            }

我已经用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);
            });

现在同一行在表中插入了三遍。任何建议/建议都值得赞赏。

因此,您具有以下变量:

string typeId, type, value;
bool isValid;

如果我运行此foreach,则任何时候typeId都会被访问一次。

foreach (var item in list)
{
    typeId = item.id;
}

现在,如果我将其并行化,那么在任何一次N线程都将访问typeId。 N个线程,可以在任何时间更新。

Parallel.ForEach(list, item =>
{
    typeId = item.id;
});

将您的临时变量移至ForEach中,以便将它们的范围限定为匿名函数:

Parallel.ForEach(list, item =>
{
    string typeId, type, value;
    bool isValid;
    typeId = item.id;
});

实际上,即使Paralle.Foreach创建了多个线程来处理并行处理,某些变量也会在其作用域之外声明,因此它们是唯一的。 尝试在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM