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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:56:04+00:00 2026-05-27T21:56:04+00:00

I need to use 60 threads in one time(parallelly) and for it I use

  • 0

I need to use 60 threads in one time(parallelly) and for it I use ThreadPool. I have exception here:

temp = 1;
for (int j = 0; j < temp; j++) {
   ThreadPool.QueueUserWorkItem(delegate(object notUsed) {
      RequestToRajons(rajs_ip[j].ToString(),rajs_name[j].ToString(), sql_str, base_str, saveFileDialog1,j); 
   });
}

It’s gives me exception that j=1(array out of range). But I have a contidion!
If I use a breakpoint with step, I haven’t got an exception.

  • 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-27T21:56:05+00:00Added an answer on May 27, 2026 at 9:56 pm

    This is the classic for/capture issue, because you are “capturing” j, and there is only one j. All your threads are processing using the same j variable; the value they see is indeterminate, but the last several threads will most likely see the exit value of the loop, i.e. one too many.

    Instead:

    for (int loopIndex = 0; loopIndex < temp; loopIndex++)
    {
        int j = loopIndex;
        // the following line has not changed at all
        ThreadPool.QueueUserWorkItem(delegate(object notUsed) { RequestToRajons(rajs_ip[j].ToString(),rajs_name[j].ToString(), sql_str, base_str, saveFileDialog1,j); });
    }
    

    it sounds silly, but you now have a j per loop iteration, because the scope of the capture depends on the declaration scope of the variable. Here, j is defined inside the loop. In the for loop, the variable is technically defined outside the loop.


    Another way to do this is to use the parameter to the thread-pool:

    for(int loopIndex = 0; loopIndex < temp; loopIndex++)
    {
        ThreadPool.QueueUserWorkItem(delegate(object ctx) {
            int j = (int) ctx;
            // stuff involving j
        }, loopIndex); // <=== see we're passing it in, rather than capturing
    }
    

    Here’s an expanded version of how “captured variables” and anonymous methods work, the over-simplified version; firstly, the compiler does this for you:

    class CaptureContext { // <== the real name is gibberish
        public int j; // yes it is a field; has to be, so `ref` and `struct` etc work
        public void SomeMethod(object notUsed) {
            RequestToRajons(rajs_ip[j].ToString(),rajs_name[j].ToString(), sql_str, base_str, saveFileDialog1,j); 
        }
        // it might also be capturing "this"; I can't tell from your example
    }
    

    and your method becomes (because j is technically defined outside the loop):

    var ctx = new CaptureContext();
    for (ctx.j = 0; ctx.j < temp; ctx.j++) {
        ThreadPool.QueueUserWorkItem(ctx.SomeMethod);
    }
    

    now; can you see that there is only one “capture” object, and that we’re using it at random points in times where ctx.j is not necessarily what we thought it was? The fix rewrites that as:

    for (int loopIndex = 0; loopIndex < temp; loopIndex++) {
        var ctx = new CaptureContext();
        ctx.j = loopIndex;
        ThreadPool.QueueUserWorkItem(ctx.SomeMethod);
    }
    

    here, can you see there is a “capture” object per iteration, which is because the j is declared inside the loop? The “what is a new capture context” depends on the scope of the variables that are captured.

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

Sidebar

Related Questions

I need help on which random number generator to use simultaneously from multiple threads
I need to work with array from several threads, so I use CRITICAL SECTION
Need to use own imaged markers instead built-in pins. I have several questions. 1.
An instance of System.Net.Sockets.Socket can be shared by 2 threads so one use the
we use One-Time initialization for pthreads like this: /* define a statically initialized pthread_once_t
I have a scenario where I use threads. Firstly I have a folder where
I need a simple one at a time lock on a section of code.
I'll have a database object that can be accessed from multiple threads as well
I have a large list (expanding over time) of URLs that I need to
Whenever I want to modify a winform from another thread, I need to use

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.