In the process of using C #, it is unavoidable to use multi-threading, and after multithreading, how the thread interacts with the interface is a very headache. In fact, not only the interface, in general, we often need to get some information about the thread to determine the state of the thread. The better way is to use the delegate implementation, see Example:
Class TestClass { //declares a delegate (delegate) type: Testdelegate, which can carry a method with a null return value and only one parameter (long). Public delegate void Testdelegate (long i); Declares an object of type Testdelegate. This object represents a method where the return value is null and the parameter has only one (long type). It can be equipped with n methods. Public testdelegate Mainthread; <summary> ////test method///</summary> public void TestFunction () { long i = 0; while (true) { i++; Mainthread (i); Call the delegate object Thread.Sleep (+); Thread waits 1000 milliseconds }
}
WinForm Interface Code:
1.///<summary> 2. button click event 3. </summary> 4. <param name= "Sender" ></param> 5. <param name= "E" ></param> 6. private void Button1_Click (object sender, EventArgs e) 7. {8. //Create Object 9 for the TestClass class. TestClass TestClass = new TestClass (); Ten. One . There are two methods on the Mainthread (delegate) object of the TestClass object, which is the equivalent of calling the Mainthread object in the thread. testclass.mainthread = new Testclass.testdelegate (REFRESHLABMESSAGE1); Testclass.mainthread + = new Testclass.testdelegate (refreshLabMessage2); . Creates a parameterless thread that executes the TestFunction method in the TestClass class. thread testclassthread = new Thread (new ThreadStart (testclass.testfunction)); 18//Start thread, the thread starts after the start of execution.
+/-<summary> 22. Update the thread execution number 23 on the interface. </summary> 24. <param name= "I" ></param> 25. private void RefreshLabMessage1 (long i) 26. {27.//Determine if the method is called by the main thread, that is, the thread that created the LabMessage1 control, and when the control's InvokeRequired property is ture, the description is called by a thread other than the main thread. If not judged, it will cause an exception of 28. if (this.labMessage1.InvokeRequired) 29. {30.//Create an object for the TestClass class again. 31. TestClass TestClass = new TestClass (); 32.//The Mainthread object for the new object is equipped with method 33. Testclass.mainthread = new Testclass.testdelegate (REFRESHLABMESSAGE1); //this refers to the form in which the Invoke method of the form is invoked, that is, the method of executing the Mainthread object delegate with the form's creation thread, plus the required parameter (i) 35. This. Invoke (testclass.mainthread,new object[] {i}); 36.} 37. Else 38. {labmessage1.text = i.ToString (); 40.} 41. } 42. +/-<summary> 44. Update the thread execution number 45 on the interface. </summary> 46. <param name= "i" ></param> 47. private void RefreshLabMessage2 (long i) 48. {49.//Ibid. 50. if (this.labMessage2.InvokeRequired) 51. {52.//Create an object for the TestClass class again. 53. TestClass TestClass = new TestClass (); 54.//The Mainthread object for the new object is equipped with method 55. Testclass.mainthread = new Testclass.testdelegate (refreshLabMessage2); //this refers to the form in which the Invoke method of the form is invoked, that is, the method of executing the Mainthread object delegate with the form's creation thread, plus the required parameter (i) 57. This. Invoke (Testclass.mainthread, new object[] {i}); 58.} 59. Else 60. {labmessage2.text = i.ToString (); 62.} 63. }
Go C # leverages delegates to update UI data across threads