2013-05-14 133 views
0

我有一个调用另一个函数的函数'graph1',如下所示。当我打电话执行访问多线程全局数组的函数

graph1threader(copy, date); 

我得到我想要的答案,但是这需要30秒以上,所以我尝试使用多线程。但是,当我使用

thread[copy] = new Thread(() => graph1threader(copy, date));//pass date,copy 
thread[copy].Start(); 

我得不到结果,即全局变量只能保持0.为什么这样?我该如何纠正这一点?

void graph1() 
    { 

     chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; 
     chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false; 

     for (int k = 0; k < 5; k++) 
     {     
      int copy = k; 
      DateTime date = G.AddDays(copy); 
      refValues[copy] = 0; 
      threshValues[copy] = 0; 
      y_Values[copy] = 0; 
      y__Values[copy] = 0; 
      yValues[copy] = 0; 

      //task[copy] = new Task(() => graph1threader(copy, date));//pass date,copy 
      //ask[copy].Start(); 
      graph1threader(copy, date); 

     } 



      for (int j = 0; j < 5; j++) 
      { 
       DateTime temp = G.AddDays(j); 
       string temper = temp.ToShortDateString(); 
       y__Values[j] = y__Values[j] - y_Values[j]; 
       xNames[j] = temper; 
      } 

     chart1.Series[1].Points.DataBindXY(xNames, y_Values); 

     chart1.Series[2].Points.DataBindXY(xNames, y__Values); 

     chart1.Series[3].Points.DataBindXY(xNames, refValues); 

     chart1.Series[4].Points.DataBindXY(xNames, threshValues); 

     chart1.Series[5].Points.DataBindXY(xNames, yValues); 

    } 

void graph1threader(int x, DateTime date) 
    { 
     DBConnect A = new DBConnect(); 
     List<string>[] list = new List<string>[4]; 
     list = A.mandetselect(); 
     int numberofmen = A.Countmandet(); 
     string[] man_name = list[0].ToArray(); 
     string[] trade = list[1].ToArray(); 
     string[] license = list[2].ToArray(); 
     string[] training = list[3].ToArray(); 
     string[] display_status = list[4].ToArray(); 
     //string abc; 
     List<string>[] lista = new List<string>[5]; 
     List<string>[] listc = new List<string>[14]; 


     for (int j = 0; j < numberofmen; j++) 
     { 
      int flag = 0; 
      if (!display_status[j].Equals("NO") && (selection == "ALL" || (selection == "LAE" && license[j] != "") || (selection == "NON LAE" && license[j] == "") || (selection == "ALL AVIONICS" && trade[j] == "Avionics") || (selection == "NON LAE AVIONICS" && trade[j] == "Avionics" && license[j] == "") || (selection == "LAE AVIONICS" && trade[j] == "Avionics" && license[j] != "") || (selection == "ALL AIRFRAMES" && trade[j] == "Airframes") || (selection == "NON LAE AIRFRAMES" && trade[j] == "Airframes" && license[j] == "") || (selection == "LAE AIRFRAMES" && trade[j] == "Airframes" && license[j] != ""))) 
      { 
       refValues[x]++; 
       threshValues[x] = 0.8 * refValues[x]; 
       string abc = man_name[j].Replace(" ", "_"); 
       int no_of_proj = A.Countproj(abc);//required 
       lista = A.manprojselect(abc);//required 
       string[] projname = lista[0].ToArray(); 
       string[] country = lista[2].ToArray(); 
       string[] startofproj = lista[3].ToArray(); 
       string[] endofproj = lista[4].ToArray(); 
       string Status = ""; 
       listc = A.Select(); 
       int numberc = A.Count();//number of projects, not required 
       string[] nameofproj = listc[0].ToArray(); 
       string[] status = listc[13].ToArray(); 

       for (int l = 0; l < A.Countproj(abc); l++) 
       { 

        for (int m = 0; m < numberc; m++) 
        { 
         if (nameofproj[m] == projname[l]) 
         { 
          Status = status[m]; 
         } 
        } 


        DateTime shuru = DateTime.ParseExact(startofproj[l], 
            "dd-MM-yyyy hh:mm:ss", 
            CultureInfo.InvariantCulture); 
        DateTime anth = DateTime.ParseExact(endofproj[l], 
            "dd-MM-yyyy hh:mm:ss", 
            CultureInfo.InvariantCulture); 
        if (date >= shuru && date <= anth) 
        { 


         if (Status != "PLANNED" && Status != "LO" && flag == 0) 
         { 
          y_Values[x]++;//BASIC UTILISATION 
          flag = 1; 
         } 
         if (Status == "IP" || Status == "OTD") 
          y__Values[x]++;//EXCESS 
         if (Status == "PLANNED") 
         { 
          yValues[x]++;//UNUTILISED 

         } 

        } 

       } 
      } 
     } 

    } 

我最近才碰到过多线程。所以请原谅,如果代码看起来不太好。 threshValue[]refValues[]y_Values[]y__Values[]yValues[]都是全局变量

+0

不,我试着再次运行代码。它工作正常,但增量值不会反映在全局变量中。但是,当我使用thread.join()时,我得到了我想要的结果,但延迟时间大约为20秒。 – 2013-05-14 08:34:47

回答

0

多线程不会自动地使你的程序运行速度更快,并Thread.Join只需等待线程来完成。

基本上,如果你的主线程无法继续,直到线程完成,你不应该使用多线程。哪里多线程倒是

例子:

  • 试图从多个位置下载资料,可以让多个线程等待数据从一个位置时,每个
  • 时要执行耗时任务,同时保持响应

用户界面中的第二个例子可能是这里的情况,但Thread.Join是阻塞操作:它仍然会阻止而线程窝被更新的用户界面国王。在这种情况下,你必须让线程通知主线程完成。

我不知道在什么平台上你在这里工作,但例如在Windows窗体中,Form类有一个Invoke方法,它可以让你打电话这种形式的线程的方法:

class TheForm 
{ 
    // the method running in a separate thread 
    void myThread() 
    { 
     // do the time consuming work 
     byte[] myData = ProcessData(); 

     // send data to the main thread 
     Invoke(new Action<byte[]>(ThreadCompleted), myData); 
    } 

    // will be executed in the main thread 
    void ThreadCompleted(byte[] data) 
    { 
     // process the data 
    } 
} 

关于全局变量和多线程:任何线程都可以访问它们,但是哪个线程访问。如果可以的话,你应该避免让多个线程访问它们,或者使用lock机制来保护它们。

0

经过大量的研究和实验。 I'vs了解到,这个问题是不是由于缺乏多线程的,而是连接过于频繁的数据库(我的环内。)

我的解决办法之前,进入到获得批量从数据库中的所有数据我循环。这节省了很多宝贵的时间。