views:

55

answers:

2

In C# Using Directory.GetFiles(), is there a way to start threads as files are found?

The behavior appears to be 'find all the files' then do what you need on each. Is there another class that allows one to start threads (up to a certain count) as files are found?

+5  A: 

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.

SLaks
make sure you wrap that baby in a try-catch, bc if you try to enumerate a file you don't have permissions to, BAM! it throws an exception.
Muad'Dib
+2  A: 

SLaks has answered this for .NET 4.0. Buf if you absolutely need this behavior in a pre-4.0 project, you can of course always do a PInvoke, using FindFirstFile and FindNextFile. They return after each file (as the names imply).

There is an example on pinvoke.net.

steinar
Oh, very nice, thank you :-)
Matt