.net – In C# Using Directory.GetFiles(), is there a way to start threads as files are found instead of having to wait? – Education Career Blog

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?

,

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 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.

Leave a Comment