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

标签 c# task-parallel-library

我正在将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);
            });


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

最佳答案

实际上,即使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);
        });

关于c# - Parallel.ForEach无法按预期的方式运行C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49189388/

相关文章:

c# - Task.Yield() 与 Task.Delay(0)

c# - 如何初始化 Task<List<string>> 参数?

c# - 将类对象转换为数据源

c# - 从对列表创建邻接列表类型结构

c# - 通过 SOAP 的 MVC 3 存储库模式

asp.net-mvc-4 - 如何使 ASP.NET MVC Controller 方法异步

c# - 如何正确地并行运行多个异步任务?

c# - 我怎样才能等到 Parallel.ForEach 完成

c# - .Net 是否为每个 dll 和 exe 创建 1 个堆栈(用于存储值类型)或每个程序创建 1 个堆栈

c# - 为什么 C# 中没有 `fieldof` 或 `methodof` 运算符?