I have one main thread, and many other background threads.
The main usage of those background threads is to query data (many queries from the web, which is why I create multiple threads: to avoid the lagging the user interface).
When it comes to exporting the data in the main thread (the user interface), I need to wait until all the other threads are finished.
My code is:
//...code to open save file dialog...
//this loop is to wait for all the threads finish their query
//QueryThread.threadCount is the count of the background threads
while (QueryThread.threadCount != 0)
{
Thread.CurrentThread.Join(1000);
Console.WriteLine(QueryThread.threadCount);
}
//...code to export data...
If I comment the while loop out, the program will run smoothly, but some of my exported data will have possibility of showing some "unwanted" material since some of the background threads haven't finished their work.
However, the above while loop is infinite, the threadCount never changes, which means during the "Join()" method, no background thread is running.
Why are the background threads blocked and how can I solve the problem?
Thanks a lot!
You are calling the Join method on the current thread which doesn't make much sense. You should call it on your worker threads:
foreach (Thread thread in workerThreads)
{
thread.Join(1000);
}
Unfortunately this kind of defeats the purpose of using threads because it will block the calling until all other threads are finished.
The RunWorkerCompleted event of a BackgroundWorker
could be used to notify the completion of a background task and perform updates on the form.
Your implementation is wrong, and you shouldn't use Join as a synchronization primitive between your threads.
What you should do is implement the producer-consumer pattern. This will allow you to have the threads waiting to do work, and then come alive to do that work when you place it in the queue.
However, the change that I would make is that from the UI thread, don't add the data directly to the queue that the producer and consumer share. Rather, make a copy of that data and then put that in the queue.
For more information on how to implement the producer-consumer pattern in .NET, I suggest you read the MSDN documentation titled "How to: Synchronize a Producer and a Consumer Thread (C# Programming Guide)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With