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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:56:59+00:00 2026-05-28T07:56:59+00:00

I am trying to run a console process against each country. All the countries

  • 0

I am trying to run a console process against each country. All the countries are in a datatable. Since the process for each country took 3-4 days. I have decided to run 4 countries at the same time to consume all 4 four processor of my quad core machine.

I write the above code, but the problem with this code is, it runs all the thread at the same time. I want a maximum of 4 thread simultaneously and the rest wait for their turn. I don’t mind if the application stuck in country loop and wait for the threads to be finished and start next thread immediately.

private void Form1_Load(object sender, EventArgs e) 
{
    DataTable dt = new DataTable("Countries");

    #region DataProcessing
    DataColumn dc1 = new DataColumn();
    dc1.DataType = System.Type.GetType("System.Int32");
    dc1.ColumnName = "CountryID";

    DataColumn dc2 = new DataColumn();
    dc2.DataType = System.Type.GetType("System.String");
    dc2.ColumnName = "CountryName";

    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);

    DataRow dr = dt.NewRow();
    dr["CountryID"] = 1;
    dr["CountryName"] = "US";
    dt.Rows.Add(dr);

    //similar code...

    dr = dt.NewRow();
    dr["CountryID"] = 20;
    dr["CountryName"] = "KR";
    dt.Rows.Add(dr);
    #endregion

    ThreadPool.SetMaxThreads(5, 5);

    foreach (DataRow drLoop in dt.Rows) 
    {
        Thread job = new Thread(new ParameterizedThreadStart(ThreadJob), 5);
        job.Start(drLoop["CountryName"].ToString());
    }
}
static void ThreadJob(Object country) 
{
    //Do Something
    MessageBox.Show(country.ToString());
}
  • 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-28T07:56:59+00:00Added an answer on May 28, 2026 at 7:56 am

    You are not using the ThreadPool- you are just starting up a Thread for each country:

    foreach (DataRow drLoop in dt.Rows) 
    {
        Thread job = new Thread(new ParameterizedThreadStart(ThreadJob), 5);
        job.Start(drLoop["CountryName"].ToString());
    }
    

    Try this instead:

    foreach (DataRow drLoop in dt.Rows) 
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadJob), drLoop["CountryName"].ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a script from the Windows XP console. (In case it's
Trying to run my program in FreeBSD OS, I have the following results: $
I'm trying to dynamically run a .jar from a C# assembly (using Process.Start(info) ).
Hi I'm trying to build/deploy war to websphere process server 7.0. and I run
I'm trying to use NServiceBus to send messages from a console process (very similar
I am trying to run a ruby debugger in Eclipse. I have not run
I'm trying to run a multi-threaded console app in VB and am having thread
I'm trying to get asp.net to run a console application on the server. I
I am trying to start a process and capture the output, have come a
Possible Duplicate: run shell command from java I'm trying to run a process with

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.