A detailed explanation of the differences between C # Invoke and BeginInvoke

Source: Internet
Author: User
Is the BeginInvoke method really a new thread to make an asynchronous call?

Refer to the following code:

public delegate void Treeinvoke ();p rivate void Updatetreeview ()
{
MessageBox.Show (System.Threading.Thread.CurrentThread.Name);
}private void Button1_Click (object sender, System.EventArgs e)
{
System.Threading.Thread.CurrentThread.Name = "Uithread";
Treeview1.begininvoke (New Treeinvoke (Updatetreeview));
}

Looking at the results of the Run, the dialog box that pops up shows Uithread, which means that the delegate that BeginInvoke invokes is executed in the UI thread at all.

Since it is executed in the UI thread, what does it say about "asynchronous execution"?

Let's look at the following code:

public delegate void Treeinvoke ();p rivate void Updatetreeview ()
{
MessageBox.Show (Thread.CurrentThread.Name);
}private void Button1_Click (object sender, System.EventArgs e)
{
Thread.CurrentThread.Name = "Uithread";
Thread th = new Thread (new ThreadStart (Startthread));
Th. Start ();
}private void Startthread ()
{
Thread.CurrentThread.Name = "Work Thread";
Treeview1.begininvoke (New Treeinvoke (Updatetreeview));
}

Then look at the results of the operation, the popup dialog box is displayed or uithread, what does this mean? This means that the delegate invoked by the BeginInvoke method is executed in the UI thread anyway.

What is the use of that BeginInvoke?

In multithreaded programming, we often want to update the interface display in the worker thread, and the method of calling interface control directly in multi-threading is wrong, the specific reason can be seen after reading my this article: How to call WinForm in multi-threading, if you are Daniel, don't look at me this article, Look directly at that article, anyway that article I did not understand how to read.

The Invoke and BeginInvoke are designed to solve this problem, which allows you to display a secure update interface in multiple threads.

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" always can only be relative, so that the UI thread is not too much burden, because the correct interface updates are always done through the UI thread, what we have to do is to do most of the operations in the worker thread, but 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.

In the code that updates the tree node, in fact the only code that works is: System.Threading.Thread.Sleep (100), which gives the UI thread the opportunity to handle the interface message, in fact, the digital ghost will complicate the problem, as long as the following code can work well.

private void Button1_click_ (object sender, System.EventArgs e)
{
TreeNode TN; for (int i=0;i<100000;i++)
{
Tn=new TreeNode (i.ToString ()); This.treeview1.nodes[0]. Nodes.Add (TN); if (i%100 = = 0) application.doevents ();
}
}

Related Article

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.