Note that there are some explanatory texts on larger screens.

plurals
  1. POChange Properties on a Form from a running Thread C#
    primarykey
    data
    text
    <p>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.</p> <p>When a thread runs, How would you go about changing the text in a label from inside the thread?</p> <p>I don't think locking the value is necessary since its not used in the main program, so deadlocks wont occur.</p> <p>What i tried, that failed.</p> <p>Having a Global variable (int = 0)</p> <p>Running a test thread to change the variable to 5.</p> <p>When i want to display that number <strong>outside my thread</strong> (messagebox.show(int.tostring()) the var is still 0</p> <p>How would you go about changing things in/on your main form from within a thread?</p> <p><strong>Code</strong></p> <p><em>This code to create a thread i did not use was this.</em></p> <pre><code> Thread threadA = new Thread(new ThreadStart(DoWork)); threadA.Start(); </code></pre> <p>When i use that way, i get the error</p> <p>"Cross-thread operation not valid: Control 'button1' accessed from a thread other than the thread it was created on"</p> <p>When i try to edit or change something on my thread.</p> <p>So i used this code.</p> <p><em>the lbldirectory is the name of my partial class.</em></p> <pre><code> // StartFileCheckProcess(); workerObject = new lbldirectory(); workerThread = new Thread(workerObject.DoWork); // the worker thread. workerThread.Start(); </code></pre> <p>DoWork is my class where i try and change the properties on the Form.</p> <p>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.</p> <pre><code>public void DoWork() { button1.Text = "Hello there"; } </code></pre> <p>So that runs the thread, and the thread works, it just cant change anything on my form.</p> <p><em><strong>MODIFIED ANSWER</em></strong> (Given by Tudor and Konstantin Vasilcov)</p> <p>On a button click event.</p> <pre><code>var t = new Thread(TEST); t.Start(); </code></pre> <p>The class that the thread starts</p> <pre><code>private void TEST() { for (int i = 0; i &lt; 10; i++) { lbl_CurrentFile.BeginInvoke( ((Action)(() =&gt; lbl_CurrentFile.Text = i.ToString()))); Thread.Sleep(1000); } } </code></pre> <p>That successfully changes the text of a label inside a thread</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload