I am trying to loop through all files and folders and perform an action on all files that have a certain extension. This method works fine, but I would like to make it multithreaded because when done over tens of thousands of files, it is really slow and I would imaging using multithreading would speed things up. I am just unsure about how to use threading in this case.
doStuff
reads properties (date modified, etc. from the files and inserts them into a sqlite database. I am starting a transaction before the scan method is called so that is optimized as much as it can be.
Answers that provide the theory on how to do it are just as good as full working code answers.
private static string[] validTypes = { ".x", ".y", ".z", ".etc" };
public static void scan(string rootDirectory)
{
try
{
foreach (string dir in Directory.GetDirectories(rootDirectory))
{
if (dir.ToLower().IndexOf("$recycle.bin") == -1)
scan(dir);
}
foreach (string file in Directory.GetFiles(rootDirectory))
{
if (!((IList<string>)validTypes).Contains(Path.GetExtension(file)))
{
continue;
}
doStuff(file);
}
}
catch (Exception)
{
}
}