C # invoke uses

Source: Internet
Author: User

  

In multithreaded programming, we often have to update the interface display in the worker thread, and the method of invoking the interface control directly in multi-threading is the wrong way, and invoke and BeginInvoke are to solve this problem, which makes you display in a secure update interface in multi-thread.

The correct approach is to encapsulate the code that involves the update interface in a worker thread as a method, called by Invoke or BeginInvoke, and the difference is that one causes the worker to wait, while the other does not.

The so-called "side response operation, one side add node" can always be relative, so the UI thread burden is not too large, because the correct interface is always to be updated through the UI thread to do, we have to do is to take the majority of operations in the worker thread, and will be the pure interface updates into the UI thread to do, This also achieves the purpose of easing the burden on the UI thread.

Give a simple example of how to use it, such as when you start a thread, and you want to update a textbox in the form in the method of the threads.

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)
{
Txtreceive.text + = str + System.Environment.NewLine;
}

Reprint: Chen Eric

C # invoke uses

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.