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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:35:30+00:00 2026-05-16T04:35:30+00:00

I’ve a little problem with this code: This is the main method of the

  • 0

I’ve a little problem with this code:

This is the “main” method of the app:

private Thread main_process;
private Clases.GestorTR processor;

public void begin()
{
   processor = new Clases.GestorTR();
   main_process = new Thread(new ThreadStart(processor.ExecuteP));
   main_process.Start();
}

I’ve created a Thread to process other “Transacction Threads” to avoid blocking the GUI.
This is the method ExecuteP, on processor object:

public void ExecuteP()
{
// Readed an DataTable with BD transacction, filled with numbers
 foreach (DataRow dr in dtResults.Rows)
 {
    int Local_number = Convert.toInt32(dr["autonum"].ToString());
    ThreadStart starter;
    starter = delegate { new QueryBD.QueryCounter(Local_number); };
    new Thread(starter).Start();
 }
}

This is QueryCounter method of QueryBD class:

....
private void QueryCounter(int _counter)
    {
        logs.log("ON QUERY_PROCESS: " + _counter);
    }
...

Now, the problem. When calling the delegate, some threads are crossing parameters. For example, in the foreach method the log shows correct (1,2,3,4,5,6,7,8) but, in the QueryCounter method (called each time with the new thread, the log shows (1,1,1,4,5,6,6,8) for example. I’ve also tried to use locks, but the problem is the same. Also testing with the ThreadPool way with the same result.
I think I’m missing something in the foreach loop, because if I debug the first run, the thread is Started, but without action in the log.

Thanks!,

  • 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-16T04:35:31+00:00Added an answer on May 16, 2026 at 4:35 am

    You should try to change some parts of your code like that:

    public void ExecuteP()
    {
      QueryBD facade = new QueryBD.
      foreach (DataRow dr in dtResults.Rows)
      {
        int Local_number = Convert.toInt32(dr["autonum"].ToString());
        new Thread(new ParameterizedThreadStart(facade.QueryCounter)).Start(Local_number);
      }
    }
    
    public void QueryCounter(object _counter)
    {
      ...
    }
    

    Hope it works.

    Btw. I’ve created one object called facade and I’m passing that object to various threads. It can also result in some side effects if there will be thread sensitive part of code in the facade object, so you can also consider locking there:

    public void QueryCounter(object _counter)
    {
       lock(this)
       {
          //
       }
    }
    

    or providing new QueryBD to each thread, but it can affect performance.

    EDIT: Hey, 4 things:

    1. While using ParametrizedThread, the variable passed to Start method of the thread (thread.Start(variable)) is copied at the time of call. Such copied variable is then used in the child thread. Anonymous delegate works different. It keeps the reference to the variable, so when the variable is used by the child thread, it can be changed by the time in your parent thread. That is why you had unpredicted behaviour.

    2. Better explanation you can find here: Differing behavior when starting a thread: ParameterizedThreadStart vs. Anonymous Delegate. Why does it matter?.

    3. The performance depends. If creation of your object is heavy (ex. it creates new connection to DB each time it is created) performance can be seriously affected by creation of many such objects – it is where lock is better. If creation of the object is light, you can create as many objects as you want. It depends.

    4. If you want your code to be run in defined order, you shouldn’t use threads at all. If you want to preserve execution order, sequential invoking is the right way – see Hans Passant explanation.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this

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.