Let me preface this with the disclaimer that I am very new to multithreading and may be missing something obvious.
I'm currently using the below code to process all the files in a directory. My question is if it would ever be possible for a thread to finish, decrement numFilesLeft
, and find it equal to 0 because the next item hasn't been added as a work item and not because all the files have been processed? If this is possible what would be the standard way to make sure it doesn't occur?
Thank you for your time.
List<Bar> bars = new List<Bar>();
int numFilesLeft = 0;
ManualResetEvent isWorkDone = new ManualResetEvent(false);
foreach (string dirName in Directory.GetDirectories(@"c:\Temp"))
{
foreach (string file in Directory.GetFiles(dirName))
{
string temp = file;
Interlocked.Increment(ref numFilesLeft);
ThreadPool.QueueUserWorkItem(delegate
{
try
{
List<Bar> results = Process(File.ReadAllText(temp));
if (results.Count > 0)
{
lock (bars) bars.AddRange(results);
}
}
finally
{
if (Interlocked.Decrement(ref numFilesLeft) == 0)
{
isWorkDone.Set();
}
}
});
}
}
isWorkDone.WaitOne();
isWorkDone.Close();