Is the BeginInvoke method really a new thread to make an asynchronous call?

Source: Internet
Author: User

Transfer from the original BeginInvoke method is it really a new thread to make an asynchronous call?

Is the BeginInvoke method really a new thread to make an asynchronous call?

Refer to the following code:

 Public Delegate voidTreeinvoke ();Private voidUpdatetreeview () {MessageBox.Show (System.Threading.Thread.CurrentThread.Name);}Private voidButton1_Click (Objectsender, System.EventArgs e) {System.Threading.Thread.CurrentThread.Name="Uithread"; Treeview1.begininvoke (NewTreeinvoke (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 voidTreeinvoke ();Private voidUpdatetreeview () {MessageBox.Show (Thread.CurrentThread.Name);}Private voidButton1_Click (Objectsender, System.EventArgs e) {Thread.CurrentThread.Name="Uithread"; Thread th = new Thread (new  ThreadStart (startthread));  th. Start ();}         Private voidStartthread () {Thread.CurrentThread.Name="Work Thread"; Treeview1.begininvoke (NewTreeinvoke (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 UI control-initiated BeginInvoke method is executed in the UI thread anyway (the original statement of the word is to be discussed because you are using treeView1 To BeginInvoke, the preceding sentence can be added as the qualifier "UI control initiated". Reprint notes).

What is the use of that BeginInvoke?

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 wrong, the specific reason can be seen after reading my this article

This article: How to call WinForm in multi-threading, if you are Daniel's words do not read my this article, directly read 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 in the worker thread that involves the update interface 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 "one-side response, adding nodes" can always be relative, making the UI thread less burdensome, because the correct update of the interface is always done through the UI thread,

What we're going to do is take the majority of the operations out of the worker thread, and we'll put the pure UI update into the UI thread, and that's going to reduce the burden on the UI thread.

In the code that updates the tree node, 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 very well.

Private voidButton1_click_ (Objectsender, System.EventArgs e)           {TreeNode tn;  for(intI=0;i<100000; i++) {tn=NewTreeNode (i.ToString ());  This. treeview1.nodes[0].                       Nodes.Add (TN); if(i% -==0) application.doevents (); }}

Is the BeginInvoke method really a new thread to make an asynchronous call?

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.