sunil shankar
sunil shankar

Reputation: 277

Using Thread.Start() on a method that takes parameters and returns values

I call a particular function on multiple threads as follows:

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() does not seem to be returning any value, as all elements of new_value hold the value 0 even after the function is called. What may be the reason? Is there a simple solution for this problem?

Upvotes: 0

Views: 97

Answers (1)

Rafael
Rafael

Reputation: 2817

The most likely cause is that graph1threader has not finished yet, one way you can workaround this is by calling thread[copy].Join() but most likely this will defeat the purpose of using threads at all, another way will be by joining the first thread only at the end of the loop, but it depends on what you want to achieve with your code.

Upvotes: 1

Related Questions