How to access the window control across threads

Source: Internet
Author: User

In multithreading, we often need to update the interface display in the working thread, but an error will be reported when the interface control method is directly called in multithreading, invoke and begininvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.

The correct method is to encapsulate the code that involves updating the interface in the working thread as a method and call it through invoke or begininvoke. The difference between the two is that one causes the worker thread to wait, while the other does not.

The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread.

For example, when you start a thread and want to update a textbox in the form in the thread method ..

Using system. Threading;

// Start a thread

Thread thread = new thread (New threadstart (dowork ));

Thread. Start ();

// Thread method

Private void dowork ()

{

This. textbox1.text = "I Am a text box ";

}

If you operate like above, there will be exceptions in vs2005 or 2008...

The correct method is to use invoke \ begininvoke

Using system. Threading;

Namespace Test

{

Public partial class form1: Form

{

Public Delegate void myinvoke (string str1, string str2 );

Public form1 ()

{

Initializecomponent ();

}

Public void dowork ()

{

Myinvoke MI = new myinvoke (updateform );

This. begininvoke (MI, new object [] {"I Am a text box", "Haha "});

}

Public void updateform (string param1, string parm2)

{

This. textbox1.text = param1 + parm2;

}

Private void button#click (Object sender, eventargs E)

{

Thread thread = new thread (New threadstart (dowork ));

Thread. Start ();

}

}

}

In multithreading, we often need to update the interface display in the working thread, but an error will be reported when the interface control method is directly called in multithreading, invoke and begininvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.

The correct method is to encapsulate the code that involves updating the interface in the working thread as a method and call it through invoke or begininvoke. The difference between the two is that one causes the worker thread to wait, while the other does not.

The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread.

For example, when you start a thread and want to update a textbox in the form in the thread method ..

Using system. Threading;

// Start a thread

Thread thread = new thread (New threadstart (dowork ));

Thread. Start ();

// Thread method

Private void dowork ()

{

This. textbox1.text = "I Am a text box ";

}

If you operate like above, there will be exceptions in vs2005 or 2008...

The correct method is to use invoke \ begininvoke

Using system. Threading;

Namespace Test

{

Public partial class form1: Form

{

Public Delegate void myinvoke (string str1, string str2 );

Public form1 ()

{

Initializecomponent ();

}

Public void dowork ()

{

Myinvoke MI = new myinvoke (updateform );

This. begininvoke (MI, new object [] {"I Am a text box", "Haha "});

}

Public void updateform (string param1, string parm2)

{

This. textbox1.text = param1 + parm2;

}

Private void button#click (Object sender, eventargs E)

{

Thread thread = new thread (New threadstart (dowork ));

Thread. Start ();

}

}

}

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.