Yesterday, I wrote a multi-threaded program. The main role is TCP Tong Xun's applet, which uses multi-threaded technology, encountering an interesting question: I am calling a form in the working thread, let it display it. . Such as form1 f = new form1 (); f.show (); this code is not a matter of time. But in the working thread, you can't turn over. My FORM1 can't move on the desktop. What nor does it respond. It's a bit like a state of a fake crash. Later, I put Form1 F; this code was placed in the main thread and instantiated, everything is normal. (It is estimated to work threads)
After reviewing MSDN, I know:
When the program is running, we have already opened a thread, a UI thread. And usual work, such as a new window, buttons, this is under the jurisdiction of the UI thread. And if we create a new thread, when this thread needs to call a new window, it is not possible to call directly under this new thread, but need to let the UI thread call the new window. What new threads do is to send a message to the UI thread to open the window. And do this, we need to use Delegate technology. If the handler of a control is not in the same thread, you need to use the control name .invoke method
The code segment is as follows:
// The code of the working thread will directly call the FormShow method defined under Form
Listenerthread ()
{
// This is a free thread. Can't call one form directly here
Formshow (); // ok
}
Delegate Void FormHandler (Dataset DS); // Declare an entrusted
/ / Define a method This method is used to display a form
Private Void FormShow // If the current thread is not a primary UI thread, return true, otherwise, return false. {frmbills fb = new frMbills (); fb.show (); fb. Display (DS);} else {formhandler fhandler = new formHandler (this.formshow); // Create a new instance point to FormShow. This.invoke (fhandler, new object [] {ds}); // Start DELEGATE}