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 9039275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:41:27+00:00 2026-06-16T09:41:27+00:00

I want to achieve the below requirement; please suggest some solution. string[] filenames =

  • 0

I want to achieve the below requirement; please suggest some solution.

string[] filenames = Directory.GetFiles("C:\Temp"); //10 files

for (int i = 0; i < filenames.count; i++)    
{
    ProcessFile(filenames[i]); //it takes time to execute    
}

I wanted to implement multi-threading. e.g There are 10 files. I wanted to process 3 files at a time (configurable, say maxthreadcount). So 3 files will be processed in 3 threads from the for loop and if any thread completes the execution, it should pick the next item from the for loop. Also wanted to ensure all the files are processed before it exits the for loop.

Please suggest best approach.

  • 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-06-16T09:41:28+00:00Added an answer on June 16, 2026 at 9:41 am

    This will do the job in .net 2.0:

    class Program
    {
    
        static int workingCounter = 0;
        static int workingLimit = 10;
        static int processedCounter = 0;
    
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles("C:\\Temp");
            int checkCount = files.Length;
            foreach (string file in files)
            {
                //wait for free limit...
                while (workingCounter >= workingLimit)
                {
                    Thread.Sleep(100);
                }
                workingCounter += 1;
                ParameterizedThreadStart pts = new ParameterizedThreadStart(ProcessFile);
                Thread th = new Thread(pts);
                th.Start(file);
            }
            //wait for all threads to complete...
            while (processedCounter< checkCount)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine("Work completed!");
        }
    
        static void ProcessFile(object file)
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToString() + " recieved: " + file + " thread count is: " + workingCounter.ToString());
                //make some sleep for demo...
                Thread.Sleep(2000);
            }
            catch (Exception ex)
            {
                //handle your exception...
                string exMsg = ex.Message;
            }
            finally
            {
                Interlocked.Decrement(ref workingCounter);
                Interlocked.Increment(ref processedCounter);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've code like that below, what I want to achieve is to generate PDF
I want to achieve below thing in vtd-xml xml modifier class. Original xml <xml>
Please guide me how can I achieve the below task. I have to monitor
i want to insert iteration elements(Signal) according my requirement like below xml output. <?xml
Please help me to achieve the below problem: I am having one view in
I want to achieve the below picture (i made it via excel). Here are
I want to achieve the two tabs as below using titanium for android. These
I want to achieve same thing like the SQL code below (it works)shown in
I want to achieve the effect that is used on this Header on this
I want to achieve this effect: When the user focus a text area within

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.