How to know if all the Thread Pool's thread are already done with its tasks?
- by mcxiand
I have this application that will recurse all folders in a given directory and look for PDF. If a PDF file is found, the application will count its pages using ITextSharp. I did this by using a thread to recursively scan all the folders for pdf, then if then PDF is found, this will be queued to the thread pool. The code looks like this:
//spawn a thread to handle the processing of pdf on each folder.
var th = new Thread(() =>
{
pdfDirectories = Directory.GetDirectories(pdfPath);
processDir(pdfDirectories);
});
th.Start();
private void processDir(string[] dirs)
{
foreach (var dir in dirs)
{
pdfFiles = Directory.GetFiles(dir, "*.pdf");
processFiles(pdfFiles);
string[] newdir = Directory.GetDirectories(dir);
processDir(newdir);
}
}
private void processFiles(string[] files)
{
foreach (var pdf in files)
{
ThreadPoolHelper.QueueUserWorkItem(
new { path = pdf },
(data) => { processPDF(data.path); }
);
}
}
My problem is, how do i know that the thread pool's thread has finished processing all the queued items so i can tell the user that the application is done with its intended task?