You can refer to the article "C # for thread-safe invocation of Windows forms controls."
There is a problem with the network connection program: Whenever a connection arrives, a new receive thread is created, and when the receive thread receives the message, it can create a new conversation window that cannot block the receipt of the next round of messages for that receiving thread. And the receiving thread is also going to display the received message on that window.
Form.ShowDialog () method pops the modal dialog box, and modal dialogs block subsequent execution of the code, causing the receiving thread to continue executing (unless the modal window is closed)
At the beginning of the solution is: through the form.show (); method, display the non-modal window
There are several issues with the non-modal dialog box:
(1) Lifetime problem: Because it is created in a round of execution of the thread, the object may be automatically removed by the garbage collection mechanism after the end of the round;
(2) Modal dialog box does not have a system message mechanism, need to write themselves; )
(3) in the code to try a bit, did not create a message mechanism, just display window. But the window is in suspended animation, it simply can't be used.
Because of the above difficulties, we can only find another way ...
Successful experiments-----Asynchronous delegates
When you look at other articles, you happen to see the Control.invoker () method, which executes a custom or system-defined delegate.
Suddenly, the delegate can be executed synchronously (blocking execution) by using the Invoke () method, and asynchronously through the BeginInvoke () method, if it is executed asynchronously, it should not block the execution of the thread ...
Thus, the following example is done:
Precautions:
Invoking the delegate through Invoke ()/begininvoke () must be called by the control that already appears, otherwise an error message appears: "Unhandled exception: System.InvalidOperationException: Before creating a window handle, You cannot invoke invoke or BeginInvoke on a control. ”
1) Open the window code:
- void Opennewform ()
- {
- Form2 newForm = new Form2 ();
- Newform.showdialog ();
- }
(2) Thread entry function
- Thread entry function
- void _threadproc ()
- {
- //Define a delegate instance that executes the open window code
- MethodInvoker mi = new MethodInvoker (Opennewform);
- BeginInvoke (MI);
- //If there is no blocking, the code should be executable
- Console.WriteLine ("The newly opened window does not execute after blocking");
- Console.ReadLine ();
- }
(3) Create, and execute threads
- Thread newthread = new Thread (new ThreadStart (_threadproc));
- Newthread.start ();
4) The output type of the Setup project is: Console Application (this will bring up both the console and the window when executing)
The results of the implementation are as follows:
Visible, the newly opened modal window does not block the execution of the thread!
Because, if it is a synchronous call, there will be no output until the new open window executes without blocking until the Form2 window is closed.
This article is not self-created, can be viewed in original: http://blog.csdn.net/xiaobai1593/article/details/7290421
C # is created in a child thread and does not block the execution form