C # Operate the control in different threads (the operation between threads is invalid, and the control is accessed from a thread other than the thread that created it)
![C # Operate the control in different threads (the operation between threads is invalid, and the control is accessed from a thread other than the thread that created it) C # Operate the control in different threads (the operation between threads is invalid, and the control is accessed from a thread other than the thread that created it)](http://up-free-imgs.azimiao.com/wp-content/uploads/2017/01/heiyi.jpg)
text
Let's first look at operating controls in the same thread
private void addText() { This.textBox1. Text+=" r nTest"; }
private void button1_Click(object sender, EventArgs e) { this.addText(); }
Let's look at the operation in different threads
private void timerAddText() { System.Timers.Timer t = new System.Timers.Timer(2000); t.Elapsed += new System.Timers.ElapsedEventHandler(callme); t.AutoReset = true; t.Enabled = true; } public void callme(object source, System.Timers.ElapsedEventArgs e) { addText(); }
TimerAddText();
resolvent
C # prohibits cross thread direct access to controls. InvokeRequired is created to solve this problem. When the InvokeRequired property value of a control is true, it indicates that a thread other than the thread that created it wants to access it. At this time, it will call new MethodInvoker (LoadGlobalImage) internally to complete the following steps. This practice ensures the security of the control. You can understand it this way. If someone wants to borrow money from you, he can take it directly in your wallet. This is too unsafe, Therefore, you must let others tell you first, and then you can take the money out of your wallet and lend it to others, so it is safe.
InvokeRequired property of control
Gets a value indicating whether the caller must call the Invoke method when making method calls to the control, because the caller is in a thread other than the thread where the control was created.
Return results:
If the System If Windows.Forms.Control.Handle is created on a thread different from the calling thread (indicating that you must call the control through the Invoke method), then it is true; Otherwise, it is false.
Control's invoke method
public object Invoke(Delegate method); //Summary: //Executes the specified delegate on the thread that owns the underlying window handle for this control. //Parameters: //method: //The delegate that contains the method to call in the thread context of the control. //Return results: //The return value of the delegate being called, or null if the delegate has no return value.
public object Invoke(Delegate method, params object[] args); //Summary: //Executes the specified delegate with the specified parameter list on the thread that owns the control's underlying window handle. //Parameters: //method: //A method delegate that takes the same number and type of parameters as those contained in the args parameter. //args: //An array of objects passed as arguments to the specified method. If this method has no parameter, the parameter can be null. //Return results: //System. Object, It contains the return value of the delegate being called; If the delegate does not return a value, it is null.
//Define a delegate private delegate void delegateAddText();
public void callme(object source, System.Timers.ElapsedEventArgs e) { //Judge whether the current process has the right to operate on the space if (textBox1.InvokeRequired) { //Make the control's parent (original thread) execute the delegate through invoke, that is, addtext textBox1.Invoke(new delegateAddText(addText)); } else { //If you have permission to operate, just call it directly addText(); } }
References