c#Parallel For Loop多线程

时间:2017-10-10 17:09:44

标签: c# multithreading loops for-loop parallel-processing

我试图将数据绑定到复制源数据表的多个gridview控件。不幸的是,这需要很长时间才能加载页面和执行代码。我尝试使用paralle for循环但我的代码中有变量,这不起作用。有没有人知道如何使用Parallel for循环与变量或任何其他方式加快这一点?在此先感谢您的帮助!

private void DeleteRows(DataTable dt)
    {
        string[] arrayHUB = new string[8];
        arrayHUB[0] = "SYS";
        arrayHUB[1] = "DFW";
        arrayHUB[2] = "DCA";
        arrayHUB[3] = "ORD";
        arrayHUB[4] = "JFK";
        arrayHUB[5] = "DEN";
        arrayHUB[6] = "PIT";
        arrayHUB[7] = "LAX";

        Parallel.For(0, arrayHUB.Length,
               index =>
               {
                   for (int i = 0; i < arrayHUB.Count(); i++)
                   {
                       string HUB = arrayHUB[i];

                       if (HUB == "SYS")
                       {
                           gvFIDSYS.DataSource = dt;
                           gvFIDSYS.DataBind();
                       }
                       else
                       {
                           try
                           {
                               DataTable dtDEPHUB = dt.Copy();

                               List<DataRow> deleteRow = new List<DataRow>();

                               for (int j = 0; j < dtDEPHUB.Rows.Count; j++)
                               {
                                   DataRow dr = dtDEPHUB.Rows[j];

                                   DateTime dtMin = DateTime.Now.AddMinutes(-9999);
                                   DateTime dtMax = DateTime.Now.AddMinutes(9999);
                                   //DateTime dtMin = DateTime.Now.AddMinutes(-1439);
                                   //DateTime dtMax = DateTime.Now.AddMinutes(1439);
                                   DateTime ActLocalOutDateTime = Convert.ToDateTime(dr["ActLocalOutDateTime"].ToString());

                                   if (ActLocalOutDateTime < dtMin)
                                   {
                                       deleteRow.Add(dr);
                                   }
                                   if (ActLocalOutDateTime > dtMax)
                                   {
                                       deleteRow.Add(dr);
                                   }                                      
                                   else
                                   {
                                       deleteRow.Add(dr);
                                   }
                                   if (dr["DEPARTURE"].ToString() == HUB)
                                   {
                                       //Do Nothing
                                   }
                                   else
                                   {
                                       deleteRow.Add(dr);
                                   }
                               }

                               foreach (DataRow dataRow in deleteRow)
                               {
                                   dataRow.Delete();
                               }

                               GridView gvFIDDEPHUB =(GridView)FindControl("gvFIDDEP" + HUB);
                               gvFIDDEPHUB.DataSource = dtDEPHUB;
                               gvFIDDEPHUB.DataBind();
                           }

                           catch (Exception ex)
                           {
                               var NLogError = LogManager.GetLogger("error");
                               NLogError.Error("DeleteRows");
                               NLogError.Error(ex);
                           }
                       }
                   }
               });
    }

0 个答案:

没有答案