I am making a tool that runs on a large directory extracting data and starts a thread per language code (the first level of files in the directory). I added a loop that stops the threads from adding to the database until all threads are finished because the tool was going into deadlock without it. However, when testing this data, the DB is storing the wrong amount of languages even though the test data is static. For example I have 67 languages, but my DB only has 48 in it. I think the issue may be that my loop for stopping the program from proceeding before the threads have stopped may be broken, ie. it is adding files to the DB before all threads have stopped, thus losing languages along the way. I don't suppose anyone has come across a similar issue or knows of a way to solve this problem? Thanks.
//get the languages from the folders
string[] filePaths = Directory.GetDirectories(rootDirectory);
for (int i = 0; i < filePaths.Length; i++)
{
string LCID = filePaths[i].Split('\\').Last();
Console.WriteLine(LCID);
//go through files in each folder and sub-folder with threads
Thread t1 = new Thread(() => new HBScanner(new DirectoryInfo(filePaths[i - 1])).HBscan());
t1.Start();
threads.Add(t1);
}
// wait for all threads to complete before proceeding
foreach (Thread thread in threads)
{
while (thread.ThreadState != ThreadState.Stopped)
{
//wait
}
}