I am scanning the folder in network drive through iteration (do while loop basically).To speed up the process I need to utilize the threading or task for each folder and get result in List having attributes path,filename,lastwriteutc,size. I have done below.
string startFolder = @"D:\Development\TempScan";
string startFolder2 = @"D:\RemotePc";
List<string> foldersToScan = new List<string>();
foldersToScan.Add(startFolder);
foldersToScan.Add(startFolder2);
foreach (var folder in foldersToScan)
{
Thread thread = new Thread(() => IterateFolder(findInfoLevel, additionalFlags, folder))
{
Name = "Thread " + folder
};
thread.Start();
Console.WriteLine(thread.Name.ToLower() + " has started");
//thread.Join();
}
// Database operation here
If I used thread.Join()
method code become synchronous and then only I can perform database operation and if i dont used thread.Join()
then database operation will be called before scanning operation.
I have also used Task as below but its also synchronous
Task task1 = Task.Run(() =>
{
foreach (var folder in foldersToScan)
{
IterateFolder(findInfoLevel, additionalFlags, folder);
}
});
task1.Wait();
How to achieve asynchronous scanning of folders one thread per folder and once I get result asynchronous database insertion?