0

我在多个线程上调用特定函数,如下所示:

int q = 0;
        for (int j = 0; j < number; j++)
        {
            int copy = q;
            int copy1 = j;
            if (!display_status[copy1].Equals("NO") && (selection == "ALL" || (selection == "ALL-LAE" && license[copy1] != "") || (selection == "ALL-NON LAE" && license[copy1] == "") || (selection == "AVIONICS -ALL" && trade[copy1] == "Avionics") || (selection == "AVIONICS-NON LAE" && trade[copy1] == "Avionics" && license[copy1] == "") || (selection == "AVIONICS-LAE" && trade[copy1] == "Avionics" && license[copy1] != "") || (selection == "AIRFRAME-ALL" && trade[copy1] == "Airframes") || (selection == "AIRFRAME-NON LAE" && trade[j] == "Airframes" && license[j] == "") || (selection == "AIRFRAME-LAE" && trade[j] == "Airframes" && license[j] != "")))
            {
                int numberofprojects = numberc;
                string[] nameofproj = listc[0].ToArray();                    
                string[] name = list[0].ToArray();//list of manpower names
                string man_name = name[copy1];//Name of manpower
                List<string>[] lista = new List<string>[5];
                string[] status = listc[13].ToArray();
                thread[copy] = new Thread(() => {new_value[copy]=graph1threader(man_name,numberofprojects, nameofproj, status);});
                thread[copy].Start();
                q++;

            }
        }

graphthreader1() 似乎没有返回任何值,因为 new_value 的所有元素即使在调用函数后也保持值 0。可能是什么原因?这个问题有简单的解决方案吗?

4

1 回答 1

1

最可能的原因是graph1threader尚未完成,解决此问题的一种方法是调用thread[copy].Join(),但这很可能会完全破坏使用线程的目的,另一种方法是仅在循环结束时加入第一个线程,但这取决于你想用你的代码实现什么。

于 2013-05-13T19:17:10.513 回答