BEGININVOKE and Invoke Meaning [Reserved]

xiaoxiao2021-03-06  44

Is the beGinInvoke method really newly opened a thread for asynchronous calls?

Refer to the following code:

Public delegate void treeinvoke ();

Private 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.beginvoke (New TreeInvoke (UpdateTreeView);

}

Look at the results of the run, the pop-up dialog is displayed UITHREAD, which means that the principal called by BeGinInvoke is executed in the UI thread.

Since it is implemented in the UI thread, what is the "asynchronous execution"?

Let's take a look at the code below:

Public delegate void treeinvoke ();

Private 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.beginvoke (New TreeInvoke (UpdateTreeView);

}

Look at the results of the run, the pop-up dialog is displayed or UITHREAD, what is explained? This shows that the entrustment called the BeginInvoke method is performed in the UI thread in any case.

What is the use of BeginInvoke?

In multi-threaded programming, we often go to update interface display in the working thread, and the method of directly calling interface controls in multi-thread is wrong, and the specific reason can be seen after reading my article. : How to call WinForm in multi-thread, if you are a big cow, don't look at me, look directly, anyway, I didn't understand it.

Invoke and BeGinInvoke are to solve this problem, so that you will display your security update interface in multi-thread.

The correct approach is to encapsulate the code of the update interface in the work thread into a method. The difference between the two is the wait for the working thread, and the other will not.

And the so-called "one-sided response operation, one-sided addition node" can only be relatively, so that the burden of the UI thread is not too big, because the correct update of the interface is always going through the UI thread, what we have to do is In the working thread, we will be able to update the pure interface to the UI thread, which reaches the purpose of mitigating the UI thread burden.

In that update tree node code, the code that is unique is: system.threading.thread.sleep (100);, it makes the UI thread have the opportunity to handle interface messages, in fact, digital ghost complicates problems As long as the following code can work very 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 ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-71372.html

New Post(0)