0

I finally managed to get multithreading successfully working on my application. Now i´d like to evaluate the results. The problem is that there is no data present as I have to wait for the threads to finish their work.

Is there is any chance to check if all of my threads have finished their work?

Here is my code that I use:

for(int i = 0; i < numberOfComputers; i++)
{
     Thread thread = new Thread(() => FillData(computers[i]));
     thread.Start();               
}
//evaluation here

I know that this is not the cleanest and best way of multi-threading but it works great so far and is easy for me to follow.

Greetings

C.M.
  • 11
  • 5
  • 2
    `works great so far...` until you need to know if a thread completed its work. I suggest switching to `Task` instead. – Dan Wilson Jul 09 '18 at 17:17
  • Not the cleanest and not the best? What makes you think that? – Thomas Weller Jul 09 '18 at 17:28
  • One possibility, if your thread exits when FIllData is finished, you can keep a list of all threads that you have created and call thread.Join() on each one. Note, if Join is called from a main (GUI) thread this can cause your GUI to have times that it seems "unresponsive" depending on how long FillData takes. – Marker Jul 09 '18 at 17:33
  • Another option is to use await/async and Parallel.Foreach. For example, you could await this: Task.Factory.StartNew( () => Parallel.ForEach(computers, FillData)); Starting N threads all at once (particularly for a large N) is usually not the best solution. Parallel.Foreach hands them off in a nice throttled fashion, no fuss, no bother – Flydog57 Jul 09 '18 at 20:39

0 Answers0