对一个接受参数并返回值的方法使用Thread.Start()

本文关键字:方法 Thread 返回值 Start 参数 一个 | 更新日期: 2023-09-27 18:13:35

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

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。原因是什么?这个问题有简单的解决办法吗?

对一个接受参数并返回值的方法使用Thread.Start()

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