Take the 2-minute tour ×

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?


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

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

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.

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);

The class that the thread starts

private void TEST()
                    ((Action)(() => lbl_CurrentFile.Text = i.ToString())));

That successfully changes the text of a label inside a thread

share|improve this question
Please show the code. There is something not right here. Threads share the address space of the containing process so they should be able to change int variables. Changing controls is a different story through. –  Tudor Oct 3 '12 at 7:32
You'd better show the code of the DoWork –  horgh Oct 3 '12 at 7:47
Ive added a plain example that's not working either. This is just to understand the principle behind this, and why it's not working inside a thread. –  Ruan Oct 3 '12 at 7:53
@Ruan you should use Invoke method of your form or your button. –  horgh Oct 3 '12 at 8:02
Exactly what I was expecting. See my answer for the Invoke/BeginInvoke explanation. –  Tudor Oct 3 '12 at 8:03
show 1 more comment

2 Answers

up vote 5 down vote accepted

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 => 
             ((Action)(() => textBox.Text = "text")));

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

share|improve this answer
Thank you, Understand now what you mean. Everything working now, and learned something. –  Ruan Oct 3 '12 at 8:25
Agree with you! –  horgh Oct 3 '12 at 13:13
add comment

If you need to access GUI from a background thread, you need to use Control.Invoke method:

public void DoWork()
    button1.Invoke(new Action(() =>
        button1.Text = "Hello there";

If you have some variables shared among several threads, each time a shared variable is accessed by one of the threads must be protected by one of the syncronization techniques explicitly. I guess there you can find a great deal of necessary information: Thread Synchronization (C# Programming Guide)

share|improve this answer
This is also a great guide to threading in C#. Everything needed about synchronization is covered. –  Michael Oct 3 '12 at 7:52
Listen thank you for your answer, if i could mark 2 as being the right answer, i would!! Thanks again, you really helped me! –  Ruan Oct 3 '12 at 8:26
@Ruan you are welcome! –  horgh Oct 3 '12 at 8:28
Here's +1 from me, both our answers were on point. :) –  Tudor Oct 3 '12 at 9:06
add comment

Your Answer


