我在多个线程上调用特定函数,如下所示:
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。可能是什么原因?这个问题有一个简单的解决方案吗?
答案 0 :(得分:1)
最可能的原因是graph1threader
还没有完成,你可以通过调用thread[copy].Join()
解决这个问题的一种方法,但很可能这会破坏使用线程的目的,另一种方式将通过仅在循环结束时加入第一个线程,但这取决于您希望用代码实现的目标。