1

I have a little c# app with multiple threads runing, but my main thread has to wait for all of threads to finish then it can do the rest. problem now is that im using .join() for each thread, this seems wait for each thread to finish then it goes to next thread, which makes app not really multi-threading and take long time to finish.

so I wonder if there is any way I can get around this problem or just a way to check if there are no more threads is active.

thanks

leppie
  • 115,091
  • 17
  • 196
  • 297
user1225072
  • 335
  • 1
  • 4
  • 11

5 Answers5

1

If you're hanging on to the Thread object, you can use Thread.IsAlive.

Alternately, you might want to consider firing an event from your thread when it is done.

jssblck
  • 529
  • 5
  • 20
  • i want to run multiple at same time, but wait for them to finish, then do the rest – user1225072 Jun 01 '13 at 03:37
  • i did use .join in main thread, but i have 20 threads, each join() actually tells main thread to wait until it finishes, so 20 threads runs one by one in that case......... – user1225072 Jun 01 '13 at 04:00
1

Thread.Join() doesn't mean your application isn't multithreaded - it tells the current thread to wait for the other thread to finish, which is exactly what you want.

Doing the following:

List<Thread> threads = new List<Thread>();

/** create each thread, Start() it, and add it to the list **/

foreach (Thread thread in threads)
{
  thread.Join()
}

will continue to run the other threads, except the current/main thread (it will wait until the other threads are done).

Just use Thread.Join()

zastrowm
  • 8,017
  • 3
  • 43
  • 63
  • i did use join(), but performance is bad, took 1 and half hr to finish all, but if i run each individual thread manually, it takes less than 10 mins. that's why I am under impression join() causing issues – user1225072 Jun 01 '13 at 04:39
  • @user1225072, you want to start all the threads, and *then* join them all. It sounds like you're joining each one right after you start it. – Joe White Jun 01 '13 at 12:33
  • If you start each thread and join it before starting the next thread, it's not multithreaded. Be sure to start all threads before joining any of them. – zastrowm Jun 01 '13 at 17:19
0

Ye, as said by Cuong Le, using Task Parallel Library would be much efficient. However, you can Create a list of Threads and then check if they are alive or not.

var threadsList = new List<Thread>();

threadsList.Add(myThread); // to add

bool areDone = true;
foreach (Thread t in threadsList) { 
    if (t.IsAlive)
    {
        areDone = false;
        break;
    }
}

if (areDone)
{
    // Everything is finished :O
}
dotINSolution
  • 224
  • 1
  • 5
0

Run multiple at same time but wanted to wait for all of them to finish, here's a way of doing the same with Parallel.ForEach:

var arrStr = new string[] {"1st", "2nd", "3rd"};
Parallel.ForEach<string>(arrStr, str =>
{
    DoSomething(str); // your custom method you wanted to use
    Debug.Print("Finished task for: " + str);
});
Debug.Print("All tasks finished");    

That was the most simplest and efficient i guess it can go if in C# 4.0 if you want all tasks to run through same method

dotINSolution
  • 224
  • 1
  • 5
0

Try using BackgroundWorker

It raises an event in the main thread (RunWorkerCompleted) after its work is done

Here is one sample from previously answered question

https://stackoverflow.com/a/5551376/148697

Community
  • 1
  • 1
Jay Patel
  • 657
  • 1
  • 6
  • 14