In. if you need to change the UI control attributes in a non-UI thread, the CLR will throw an exception and prompt that the control (cross-thread operation not valid) on the interface cannot be updated in a non-UI thread ). Generally, there are two solutions. The first is to set the static property checkforillegalcrossthreadcils of control to false, as follows:
 
 
 Public form1 () {initializecomponent (); control. checkforillegalcrossthreadcils = false;} private void button#click (Object sender, eventargs e) {thread = new thread () => {for (INT I = 0; I <100000; I ++) {label1.text = I. tostring (); label1.refresh () ;}}); thread. start ();} 
 
 
Another method is to use delegation to determine whether the update operation of the current control is in another thread Based on the invokerequired property of the control. If yes, use the delegate to call the method and update the control. However, this method has the disadvantage of creating separate delegates and methods for the attribute setting method of each control. These delegates and methods are only used for cross-thread operations. For example, when you need to modify the text of a label in another thread, you need to create a setlabeltext method. Suppose you still need to update the text of the textbox, you need to create another settextboxtext method.
 
 
 
Through the following delegation and method definition, we implement "one definition, multiple use ". See:
 
 
Private delegate void parameterizedcontrolupdate (Params object [] ARGs); Private delegate void callback (component C, parameterizedcontrolupdate callback, Params object [] ARGs); Private void callback (component C, callback, params object [] ARGs) {control target = (C is control )? (C as control): This; If (target. invokerequired) {controlupdatedelegate d = delegatedcontrolupdate; target. invoke (D, new object [] {C, callback, argS}) ;}else {callback (ARGs );}} 
 
 
The preceding example can be changed:
 
 
 private void button#click (Object sender, eventargs e) {thread = new thread () =>{ for (INT I = 0; I <100000; I ++) {delegatedcontrolupdate (label1, argS => {label1.text = (string) ARGs [0]; label1.refresh () ;}, I. tostring () ;}}); thread. start () ;}