Skip to main content
 首页 » 编程设计

c#之多线程无法正常执行

2024年02月13日8linjiqin

我这里有一个函数在多线程中调用另一个函数。在第二个函数中,refValuesd[,] 的每个元素的值为 1。但是当我在调用多个线程后检查 graph1() 函数中同一二维数组的元素时,我得到不同的元素值refValuesd[,].我是多线程的新手。

void graph1() 
    { 
        for (int j = 0; j < 366; j++) //loop to refresh element values 
        { 
            refValues[j] = 0; 
            threshValues[j] = 0; 
            y_Values[j] = 0; 
            y__Values[j] = 0; 
            yValues[j] = 0; 
            for (int k = 0; k < 1000; k++) 
            { 
                threshValuesd[k,j] = 0; 
                refValuesd[k,j] = 0; 
                y__Valuesd[ k,j] = 0; 
                y_Valuesd[k,j] = 0; 
                yValuesd[k,j] = 0; 
            } 
 
        } 
 
        List<string>[] list = new List<string>[4];//manpower details 
        list = A.mandetselect(); 
        int number = A.Countmandet();//retuns an integer value 
        string[] trade = list[1].ToArray(); 
        string[] license = list[2].ToArray(); 
        string[] training = list[3].ToArray(); 
        string[] display_status = list[4].ToArray(); 
 
        List<string>[] listc = new List<string>[14];//Project details 
        listc = A.Select(); 
        int numberc = A.Count(); 
 
        string abc = ""; 
        int q = 0; 
        for (int j = 0; j < number; j++) 
        { 
 
            if (!display_status[j].Equals("NO") && (selection == "ALL" || (selection == "ALL-LAE" && license[j] != "") || (selection == "ALL-NON LAE" && license[j] == "") || (selection == "AVIONICS -ALL" && trade[j] == "Avionics") || (selection == "AVIONICS-NON LAE" && trade[j] == "Avionics" && license[j] == "") || (selection == "AVIONICS-LAE" && trade[j] == "Avionics" && license[j] != "") || (selection == "AIRFRAME-ALL" && trade[j] == "Airframes") || (selection == "AIRFRAME-NON LAE" && trade[j] == "Airframes" && license[j] == "") || (selection == "AIRFRAME-LAE" && trade[j] == "Airframes" && license[j] != ""))) 
            { 
                int z = numberc; 
                string[] nameofproj = listc[0].ToArray(); 
                int copy = q; 
                int copy2 = j; 
                string a = abc; 
                string[] name = list[0].ToArray(); 
                List<string>[] lista = new List<string>[5]; 
                string[] status = listc[13].ToArray(); 
                thread[copy] = new Thread(() => graph1threader(a, name[copy2], lista, z, nameofproj, status, copy)); 
                thread[copy].Start(); 
 
                q++; 
 
            } 
        } 
 
 
        for (int j = 0; j < 366; j++) 
        { 
            for (int k = 0; k < q; k++) 
            { 
                threshValues[j] += threshValuesd[k, j];                    
                refValues[j] += refValuesd[k, j]; 
                y__Values[j] += y__Valuesd[k, j]; 
                y_Values[j] += y_Valuesd[k, j]; 
                yValues[j] += yValuesd[k, j]; 
            } 
        } 
 
 
            for (int j = 0; j < 366; 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(string abc,string nameofj,List<string>[] lista,int numberc,string[] nameofproj,string[] status,int copy ) 
    { 
        DBConnect A = new DBConnect(); 
        int x = copy; 
        string[] projname; 
        string[] country; 
        string[] start; 
        string[] end; 
        abc = nameofj.Replace(" ", "_"); 
        lista = A.manprojselect(abc); 
        projname = lista[0].ToArray(); 
        country = lista[2].ToArray(); 
        start = lista[3].ToArray(); 
        end = lista[4].ToArray(); 
 
        for (int k = 0; k < 366; k++)//basic 
        { 
 
            refValuesd[x, k]++; 
             refValuesd[copy,k].ToString()); 
            threshValuesd[x, k] = 0.8; 
            string Status = ""; 
            int flag = 0; 
 
            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(start[l], 
                               "dd-MM-yyyy hh:mm:ss", 
                               CultureInfo.InvariantCulture); 
                DateTime anth = DateTime.ParseExact(end[l], 
                               "dd-MM-yyyy hh:mm:ss", 
                               CultureInfo.InvariantCulture); 
                if (temp >= shuru && temp <= anth) 
                { 
                    if (Status != "PLANNED" && Status != "LO" && flag == 0) 
                    { 
                        y_Valuesd[x,k]++;//BASIC UTILISATION 
                        flag = 1; 
                    } 
                    if (Status == "IP" || Status == "OTD") 
                        y__Valuesd[x,k]++;//EXCESS 
                    if (Status == "PLANNED") 
                    { 
                        yValuesd[x,k]++;//UNUTILISED 
 
                    } 
 
                } 
 
            } 
 
 
        } 
 
    } 

请您参考如下方法:

那里可能有一些问题,但我可以发现两个:

首先,线程正在尝试做 refValuesd[x, k]++这不是线程安全的。

试试这个:

Interlocked.Increment(ref refValuesd[x, k]); 

其次,您无需等待所有线程终止后再使用它们生成的数据。尝试在 for (int j = 0; j < 366; j++) 之前添加它行:

foreach (var thread in threads) 
    thread.Join(); 

看来你还有很多东西要学,所以我建议你阅读这本免费电子书:

http://www.albahari.com/threading/