c# - Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on -




i have scenario. (windows forms, c#, .net)

  1. there main form hosts user control.
  2. the user control heavy data operation, such if directly call usercontrol_load method ui become nonresponsive duration load method execution.
  3. to overcome load data on different thread (trying change existing code little can)
  4. i used background worker thread loading data , when done notify application has done work.
  5. now came real problem. ui (main form , child usercontrols) created on primary main thread. in load method of usercontrol i'm fetching data based on values of control (like textbox) on usercontrol.

the pseudocode this:

code 1

usercontrl1_loaddatamethod() {     if (textbox1.text == "myname") // gives exception     {         //load data corresponding "myname".         //populate globale variable list<string> binded grid @ later stage.     } } 

the exception gave was

cross-thread operation not valid: control accessed thread other thread created on.

to know more did googling , suggestion came using following code

code 2

usercontrl1_loaddatamethod() {     if (invokerequired) // line #1     {         this.invoke(new methodinvoker(usercontrl1_loaddatamethod));         return;     }      if (textbox1.text == "myname") // wont give exception     {     //load data correspondin "myname"         //populate globale variable list<string> binded grid @ later stage     } } 

but but... seems i'm square one. application again become nonresponsive. seems due execution of line #1 if condition. loading task again done parent thread , not third spawned.

i don't know whether perceived right or wrong. i'm new threading.

how resolve , effect of execution of line#1 if block?

the situation this: want load data global variable based on value of control. don't want change value of control child thread. i'm not going ever child thread.

so accessing value corresponding data can fetched database.

as per prerak k's update comment (since deleted):

i guess have not presented question properly.

situation this: want load data global variable based on value of control. don't want change value of control child thread. i'm not going ever child thread.

so accessing value corresponding data can fetched database.

the solution want should like:

usercontrl1_loaddatamethod() {     string name = "";     if(textbox1.invokerequired)     {         textbox1.invoke(new methodinvoker(delegate { name = textbox1.text; }));     }     if(name == "myname")     {         // whatever     } } 

do serious processing in separate thread before attempt switch control's thread. example:

usercontrl1_loaddatamethod() {     if(textbox1.text=="myname") //<<======now wont give exception**     {         //load data correspondin "myname"         //populate globale variable list<string>         //bound grid @ later stage         if(invokerequired)         {             // after we've done processing,              this.invoke(new methodinvoker(delegate {                 // load control appropriate data             }));             return;         }     } } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -