Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3242878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:01+00:00 2026-05-17T18:22:01+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:22:01+00:00Added an answer on May 17, 2026 at 6:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I save all files in a directory using utf-8? There is a
I want to find all excel files within a directory structure using recursion. The
I'm using a basic Directory.GetFiles to find the files i want to use. But
Directory.GetFiles(targetDirectory); Using the above code we get the names( i.e full path) of all
I'm using Directory.GetFiles() to list files according to given pattern. This works fine for
How can I read all jpeg files in a directory using a Filestream so
I am using this to get all files in a directory: string[] files =
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
Apparently I can't move files on different volumes using Directory.Move. I have read that
I am attempting to compare files in a directory using SML. Using the TextIO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.