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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:59:09+00:00 2026-06-12T04:59:09+00:00

I found that running a thread is really isolated to a big degree from

  • 0

I found that running a thread is really isolated to a big degree from the main application. You cant change variables (globally declared in your class) and you cant change form properties or anything by simply giving them new values within your thread.

When a thread runs, How would you go about changing the text in a label from inside the thread?

I don’t think locking the value is necessary since its not used in the main program, so deadlocks wont occur.

What i tried, that failed.

Having a Global variable (int = 0)

Running a test thread to change the variable to 5.

When i want to display that number outside my thread (messagebox.show(int.tostring()) the var is still 0

How would you go about changing things in/on your main form from within a thread?

Code

This code to create a thread i did not use was this.

        Thread threadA = new Thread(new ThreadStart(DoWork));
        threadA.Start();

When i use that way, i get the error

“Cross-thread operation not valid: Control ‘button1’ accessed from a thread other than the thread it was created on”

When i try to edit or change something on my thread.

So i used this code.

the lbldirectory is the name of my partial class.

            // StartFileCheckProcess();
            workerObject = new lbldirectory(); 
            workerThread = new Thread(workerObject.DoWork);

            // the worker thread.
            workerThread.Start();

DoWork is my class where i try and change the properties on the Form.

This is the DoWork class. Its really a plain class, but its just to understand the why this happens, and for other people just to understand the reason why and how they can overcome this.

public void DoWork()
        {
            button1.Text = "Hello there";
        }

So that runs the thread, and the thread works, it just cant change anything on my form.

MODIFIED ANSWER (Given by Tudor and Konstantin Vasilcov)

On a button click event.

var t = new Thread(TEST);
    t.Start();

The class that the thread starts

private void TEST()
            {
                for (int i = 0; i < 10; i++)
                {
                    lbl_CurrentFile.BeginInvoke(
                    ((Action)(() => lbl_CurrentFile.Text = i.ToString())));
                    Thread.Sleep(1000);
                }
            }

That successfully changes the text of a label inside a thread

  • 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-12T04:59:11+00:00Added an answer on June 12, 2026 at 4:59 am

    Ok there are two things to mind here:

    1.Changing GUI controls: only the GUI thread should make changes to controls. Other threads need to forward their updates to the GUI thread via Invoke/BeginInvoke e.g.:

     var t = new Thread(
        o => 
        {
             textBox.BeginInvoke(
                 ((Action)(() => textBox.Text = "text")));
        });
    

    2.Changing scalar values or references: this can be normally done with no problems, but be wary of data races.

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

Sidebar

Related Questions

I'm running a test, and found that the file doesn't actually get written until
I'm working on my dedicated server running CentOS. I found out that one of
I'm running Windows 7 with UAC enabled . I've always found it weird that
I found that HTMLOptionsCollection object inherits properties and methods from HTMLCollection. HTMLOptionsCollections->HTMLCollection->Object->null Is there
I found that most tutorials use one big router. For example: https://github.com/thomasdavis/backboneboilerplate/blob/gh-pages/js/router.js Wouldn't it
I need to built a thread/process that is always running, and can be queried
I'm testing ASP.NET (.NET 4) web-application under high load and have found that under
Concerning my previous question , I found out that maven can't really output jboss
Here's the situation, I have a thread running that is partially controlled by code
I have database cleanup that I need to do within a running service thread.

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.