In a win form application, I have an array of threads which are started like this:
bool stop = false;
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
threads[i] = new Thread(new ThreadStart(Job));
// How to make sure all threads have exited, when the boolean = false
void Job()
{
while (!stop)
// Do something
}
Now if user press STOP, the boolean value for stop will set to true, so threads exit the Job method one after another. How can I make sure all threads are exited?
NOTE: I need traditional threading for my case and TaskLibrary doesn’t fit my scenario.
Have you thought about using BackgroundWorkers instead? You said “traditional threads”..I’m not exactly sure what you mean so I don’t know if this is a valid proposal or not, but here it is anyways in case
Join()doesn’t solve your problemThis should prevent your form from closing if any threads are still running.