C # is created in a child thread and does not block the execution form

Source: Internet
Author: User

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:

    1. void Opennewform ()
    2. {
    3. Form2 newForm = new Form2 ();
    4. Newform.showdialog ();
    5. }

(2) Thread entry function

    1. Thread entry function
    2. void _threadproc ()
    3. {
    4. //Define a delegate instance that executes the open window code
    5. MethodInvoker mi = new MethodInvoker (Opennewform);
    6. BeginInvoke (MI);
    7. //If there is no blocking, the code should be executable
    8. Console.WriteLine ("The newly opened window does not execute after blocking");
    9. Console.ReadLine ();
    10. }

(3) Create, and execute threads

    1. Thread newthread = new Thread (new ThreadStart (_threadproc));
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.