C # Use of the multi-thread invoke Method

Source: Internet
Author: User

In multi-threaded programming, we often need to update the interface display in the working thread. In multithreading, it is wrong to call interface controls directly, 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 update the interface in the work thread.CodeEncapsulated as a method, called 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.

Here is a simple example to illustrate how to use it. For example, when you start a thread, you want to update a textbox in the form in the thread method ..

Using system. Threading;

Public Delegate void myinvoke (string Str );
Private void btnstartthread_click (Object sender, eventargs E)
{
Thread thread = new thread (New threadstart (doword ));
Thread. Start ();
}
Public void doword ()
{
Myinvoke MI = new myinvoke (settxt );
Begininvoke (MI, new object [] {"ABC "});
}

Public void settxt (string Str)
{< br> txtreceive. Text + = STR + system. environment. newline;
}

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.