1

I have been looking around for about 3 hours and can not get this invoke to work. I need the invoke because whats calling it is in a different thread and says its unstable.

Here's what I'm calling (I call it like this textBox1_TextChanged(null, null);):

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
     if(this.InvokeRequired)
     {
        this.Invoke(this?WHAT GOES HERE, null); // I know it should be a delegate or something but I can't change this to that
     }
     else
     {
        string temp = ""; 
        temp += TextToAdd;
        textBox1.Text = "s";
     }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    TIP: Generally you want to avoid using `Invoke` because 1) it blocks the worker thread 2) Can lead to thread deadlock. Consider using `BeginInvoke` instead as per Jonas's fine answer below –  Jan 28 '16 at 07:20

1 Answers1

1

You can use BeginInvoke to update the UI from other Thread.

if (this.InvokeRequired)
{
  var action = new Action(() => textBox1.Text = "s");
  this.BeginInvoke(action);
}
Jonas W
  • 3,200
  • 1
  • 31
  • 44
  • thank you this worked but some times this happens http://piclair.com/w77is do you know how to fix this. Also thanks you again lol – Nolan Vannoy Jan 28 '16 at 07:25
  • Can't say so much about that picture, but it seems like it's the UI thread that comes that way, if the method is called from the UI Thread, no invoke is required. InvokeRequired from MSDN "Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on." – Jonas W Jan 28 '16 at 07:29
  • lol i put in in a try and that seemed to fix it but ya im still getting use to vs, but i think its fixed – Nolan Vannoy Jan 28 '16 at 07:31