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!