Created
March 18, 2023 13:24
-
-
Save TorstenC/e741fe69f171a1771021cbebf4f2487e to your computer and use it in GitHub Desktop.
Traverses a directoty tree with muptiple threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var checkPath = new DirectoryInfo(startPath); | |
//HACK Paths should be an IEnummerable | |
var f = new FoundDir(checkPath.FullName, FoundDir.RootDir, 0); | |
ConcurrentQueue<FoundDir> directories = new(); | |
directories.Enqueue(f); | |
Task[] parallelTasks = { Task.Run(() => DirScan(3)), Task.Run(() => DirScan(2)), Task.Run(() => DirScan(1)) }; | |
Task.WhenAll(parallelTasks).Wait(); | |
async Task DirScan(long instance) | |
{ | |
while (instance > directories.Count) | |
{ // Wait until enough items in the queue, otherwise the Task would finish immediately | |
Thread.Yield(); | |
} //HACK Here has to be cancelled, if the queue never gets long enough | |
Console.WriteLine($"{instance:D2}: instance started"); | |
while (directories.TryDequeue(out var directoryItem)) | |
{ | |
Console.WriteLine($"{instance:D2}: {directoryItem.Outline} {directoryItem.FullPath}"); | |
try | |
{ | |
var index = 0; | |
foreach (string subDir in Directory.GetDirectories(directoryItem.FullPath)) | |
{ | |
directories.Enqueue(new FoundDir(subDir, directoryItem, index++)); | |
Thread.Yield(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine(ex); | |
} | |
//HACK here has to be waited: Does the last item has subdirectories? | |
} | |
Console.WriteLine($"{instance:D2}: instance finished"); | |
} | |
/// <summary>A <see cref="System.IO.FileSystemInfo"/> stores the full path twice: OriginalPath and FullPath. | |
/// We want to store only the name, the parent folder (and a hash value?). </summary> | |
public class FoundDir | |
{ | |
public FoundDir(string path, FoundDir parent, long index) | |
{ // ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment