You need to call Directory.EnumerateFiles
, which is new to .Net 4.0.
This method returns a lazy enumerable; each time you call MoveNext()
, it will query the filesystem for the next file.
You can start your threads like this:
foreach(var dontUse in Directory.EnumerateFiles(...)) {
string path = dontUse; //Create a new variable for each closure
ThreadPool.QueueUserWorkItem(delegate {
...
});
}
The temporary variable is necessary to ensure that each delegate closure references a separate variable; otherwise, they would all share the same value, causing trouble.