What happens at control invoke function?

Posted by user65909 on Programmers See other posts from Programmers or by user65909
Published on 2012-09-23T16:28:12Z Indexed on 2012/09/23 21:49 UTC
Read the original article Hit count: 324

Filed under:
|

A question about form controls invoke function.
Control1 is created on thread1. If you want to update something in Control1 from thread2 you must do something like:

delegate void SetTextCallback(string txt);  

void setText(string txt)  
{  
  if (this.textBox1.InvokeRequired)  
  {  
     SetTextCallback d = new SetTextCallback(setText);  
     this.Invoke(d, new object[] { txt });  
  }  
  else  
  {   
     // this will run on thread1 even when called from thread2
     this.textBox1.AppendText(msg);  
  }  
}`  

What happens behind the scenes here?

This invoke behaves different from a normal object invoke. When you want to call a function in an object on a specific thread, then that thread must be waiting on some queue of delegates, and execute the incoming delegates.

Is it correct that the windows forms control invoke function is completely different from the standard object invoke function?

© Programmers or respective owner

Related posts about c#

Related posts about thread