i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Winform controls cross thread problem
08-18-2011, 05:32 PM (This post was last modified: 08-18-2011 05:35 PM by haider.ali.)
Post: #1
Winform controls cross thread problem
If you see this error

Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.


then you are accessing your control from another thread or you are doing some asynchronous call

to solve the cross-thread issue you just have to write this

Code:
if (this.progressBar1.InvokeRequired)
  {
progressBar1.Invoke((Action)(()=>this.progressBar1.Value = 40));              
   }
else
  {
      progressBar1.Value = 40;
  }



or simply

Code:
progressBar1.Invoke((Action)(()=>this.progressBar1.Value = 40));


for textbox cross-thead problem
Code:
twxtbox1.Invoke((Action)(()=>textbox1.Text = "call is successful"));

will do it.



Action encapsulate a method that does not take any argument and does not return any value perfect for our need


Quote


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Using Thread read data of the gridView1 ? dongtrien 0 502 08-25-2016 01:29 AM
Last Post: dongtrien
  why do not SetFocus row for the GridView when using the Thread ? dongtrien 0 1,229 12-10-2015 08:16 AM
Last Post: dongtrien
  Set MDI Parent Form in winform c# nisar87 1 2,839 01-18-2013 05:20 AM
Last Post: magnum_2007
  Send bulk emails with thread asp.net nisar87 1 17,731 08-08-2011 10:47 AM
Last Post: Shafiur Rahman



User(s) browsing this thread: 1 Guest(s)