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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:32:12+00:00 2026-06-12T10:32:12+00:00

Why would this happen? You start up a thread, and it runs a class.

  • 0

Why would this happen?

You start up a thread, and it runs a class.

Inside that class there is a for loop, inside the for loop there is a “write to messagebox” and another class.

Why does the order in witch I put the write and the class (which one happens first) effect the accuracy of my data?

(This is inside the class that the thread is running)

int atPDFNumber= 0
foreach (var z in q)
        {
                atPDFNumber++;

                convertToImage(z.FullName);

                txtboxtest.BeginInvoke(
            ((Action)(() => txtboxtest.Text += atPDFNumber.ToString())));
}

That output gives me some overlapping values. the output is

Run 1 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Run 2 : 1 3 3 4 5 6 7 8 9 10 11 12 13 14

Run 2 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14

Where this Runs them all in the correct order

foreach (var z in q)
            {
                    atPDFNumber++;

                txtboxtest.BeginInvoke(
                ((Action)(() => txtboxtest.Text += atPDFNumber.ToString())));

               convertToImage(z.FullName);

Why does that happen? It might be that the external method i use in the class ConvertToImage is also running a thread.. in that case, how would i “pause” my thread to wait for the ConvertToImage to finish…

Other wise i have no idea why this thread acts the way it does?

  • 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-12T10:32:14+00:00Added an answer on June 12, 2026 at 10:32 am

    Your action is capturing the value of atPDFNumber variable, not the current value of it. In your example if you assing atPDFNumber to a local variable and pass it to your action it will work as expected.

    int atPDFNumber = 0; 
    foreach (var z in q)
    {
        atPDFNumber++; 
        convertToImage(z.FullName);
        int currentValue = atPDFNumber;
        txtboxtest.BeginInvoke(((Action)(() => txtboxtest.Text += currentValue.ToString())));
    }
    

    Here you can find about Variable Capturing in C#

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

Sidebar

Related Questions

Would anyone happen to know a trick that will keep this MSBuild task from
I'm surprised this doesn't happen automatically, but I would like my applications window to
Would this be the correct way of declaring that an XML element Cluster contains
I have read that you can do it, but would this really improve performance
*I'm using Java. I have this thread, agent, that explores a room to determine
what would happen if I spin off a thread to execute a long-running process
Would this be a proper way to dispose of a BackGroundWorker? I'm not sure
Why would this work timeout 10s echo foo bar # foo bar but this
For a login system in php would this be a suitable outline of how
In a single paged application, would this be the best way to bind and

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.