Note that there are some explanatory texts on larger screens.

plurals
  1. POCross-thread operation not valid: Control accessed from a thread other than the thread it was created on
    primarykey
    data
    text
    <p>I have a scenario. (Windows Forms, C#, .NET)</p> <ol> <li>There is a main form which hosts some user control.</li> <li>The user control does some heavy data operation, such that if I directly call the <code>UserControl_Load</code> method the UI become nonresponsive for the duration for load method execution.</li> <li>To overcome this I load data on different thread (trying to change existing code as little as I can) </li> <li>I used a background worker thread which will be loading the data and when done will notify the application that it has done its work. </li> <li>Now came a real problem. All the UI (main form and its child usercontrols) was created on the primary main thread. In the LOAD method of the usercontrol I'm fetching data based on the values of some control (like textbox) on userControl. </li> </ol> <p>The pseudocode would look like this:</p> <p><strong>CODE 1</strong></p> <pre><code>UserContrl1_LoadDataMethod() { if (textbox1.text == "MyName") // This gives exception { //Load data corresponding to "MyName". //Populate a globale variable List&lt;string&gt; which will be binded to grid at some later stage. } } </code></pre> <p>The Exception it gave was</p> <blockquote> <p>Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.</p> </blockquote> <p>To know more about this I did some googling and a suggestion came up like using the following code</p> <p><strong>CODE 2</strong></p> <pre><code>UserContrl1_LoadDataMethod() { if (InvokeRequired) // Line #1 { this.Invoke(new MethodInvoker(UserContrl1_LoadDataMethod)); return; } if (textbox1.text == "MyName") // Now it wont give an exception { //Load data correspondin to "MyName" //Populate a globale variable List&lt;string&gt; which will be binded to grid at some later stage } } </code></pre> <p>BUT BUT BUT... it seems I'm back to square one. The Application again become nonresponsive. It seems to be due to the execution of line #1 if condition. The loading task is again done by the parent thread and not the third that I spawned.</p> <p>I don't know whether I perceived this right or wrong. I'm new to threading.</p> <p>How do I resolve this and also what is the effect of execution of Line#1 if block?</p> <p><strong>The situation is this</strong>: I want to load data into a global variable based on the value of a control. I don't want to change the value of a control from the child thread. I'm not going to do it ever from a child thread. </p> <p>So only accessing the value so that the corresponding data can be fetched from the database.</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.
 

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