C # invoke and BeginInvoke differences

Source: Internet
Author: User

Transferred from http://www.cnblogs.com/c2303191/articles/826571.html

Control.Invoke method (Delegate) : executes the specified delegate on the thread that owns the underlying window handle of this control.

Control.BeginInvoke method (Delegate): Executes the specified delegate asynchronously on the thread on which the control's underlying handle was created .

(i) the Invoke and BeginInvoke of control
We should base our understanding on the following:
(1) The Invoke and BeginInvoke of control are different from the invoke and BeginInvoke of delegate.
(2) The control's invoke and BeginInvoke parameters are delegate, and the delegate's method is executed on the control thread, which is what we normally call the UI thread.

We look at the code (a) (control's Invoke)
Private delegate void Invokedelegate ();
private void InvokeMethod () {
C Code Snippet
}
private void Butinvoke_click (object sender, EventArgs e) {
A code snippet .....
This. Invoke (New Invokedelegate (InvokeMethod));
B Code Snippet ...
}
What do you think is the sequence of code execution? Remember that control's invoke and BeginInvoke are all executed on the main thread, the UI thread
A------>c---------------->b
Explanation: (1) A after execution on the UI thread, start Invoke,invoke is synchronized
(2) Code snippet B does not execute, but rather immediately executes the InvokeMethod method on the UI thread, that is, code snippet C.
(3) After the InvokeMethod method has finished executing, code snippet C continues on the UI thread.

Look at the Code (ii), control BeginInvoke
Private delegate void Begininvokedelegate ();
private void Begininvokemethod () {
C Code Snippet
}
private void Butbegininvoke_click (object sender, EventArgs e) {
A code snippet .....
This. BeginInvoke (New Begininvokedelegate (Begininvokemethod));
B Code Snippet ...
}
What do you think is the sequence of code execution? Remember that control's invoke and BeginInvoke are all executed on the main thread, the UI thread
A--->c--->b (verified)

Explanation:: (1) A starts BeginInvoke after execution on the UI thread.

(2) The main UI thread is blocked at this time, calling code snippet C
(3) Code snippet C execution completes, the main UI thread continues to execute code b

From this we know:
The control's invoke and BeginInvoke delegate method is executed on the main thread, the UI thread. That is, if your delegate method is used to take long time data and then update the interface, do not invoke Control.Invoke and Control.BeginInvoke on the UI thread, as these are still blocking the UI thread, causing the interface to suspend animation.

So, what does this asynchrony mean?

Asynchronous refers to a thread that is asynchronous relative to the calling BeginInvoke, rather than to the UI thread, and you call BeginInvoke on the UI thread, of course. ----excerpt from the commentary in the article "the true meaning of Invoke and BeginInvoke".
The BeginInvoke principle is to marshal the invoked method into a message, and then call RegisterWindowMessage () in the Win32 API to send a message to the UI window. ----excerpt from the commentary in the article "the true meaning of Invoke and BeginInvoke".

(b) We use thread to invoke BeginInvoke and invoke
We open a thread, let the thread perform some time-consuming operations, and then use Control.Invoke and Control.BeginInvoke back to the user UI thread to perform the interface update.

Code (iii) Invoke of thread invoke control
Private Thread Invokethread;
Private delegate void Invokedelegate ();
private void Startmethod () {
C Code Snippet ...
Control.Invoke (New Invokedelegate (InvokeMethod));
D Code Snippet ...
}
private void InvokeMethod () {
E Code Snippet
}
private void Butinvoke_click (object sender, EventArgs e) {
A code snippet .....
Invokethread = new Thread (new ThreadStart (Startmethod));
Invokethread.start ();
B Code Snippet ...
}
What do you think is the sequence of code execution? Remember that control's invoke and BeginInvoke are all executed on the main thread, the UI thread
A------> (b and Startmethod C on simultaneous)----> (c is done, regardless of whether B is executed, Invokethread Marshals (Invoke) to the UI thread and waits for itself)---- After the >UI thread finishes processing the Butinvoke_click message, it handles the message Invokethread marshaled, executes the InvokeMethod method, that is, code snippet E, which switches the UI thread back to the Invokethread thread.
This control.invoke is synchronized relative to the invokethread thread, preventing it from running.

Explain:
1. UI execution A
2. The UI thread Invokethread,b and C execute simultaneously, B executes on the thread UI, and C executes on the thread invokethread.
3. Invokethread the message to the UI, and then waits for itself, after the UI processes the message, processing the message of the invokethread marshaling, that is, code snippet E
4. After the UI finishes E, go to thread invokethread, Invokethread thread executes code snippet D

Code (iv) BeginInvoke of thread invoke control
Private Thread Begininvokethread;
Private delegate void Begininvokedelegate ();
private void Startmethod () {
C Code Snippet ...
Control.BeginInvoke (New Begininvokedelegate (Begininvokemethod));
D Code Snippet ...
}
private void Begininvokemethod () {
E Code Snippet
}
private void Butbegininvoke_click (object sender, EventArgs e) {
A code snippet .....
Begininvokethread = new Thread (new ThreadStart (Startmethod));
Begininvokethread. Start ();
B Code Snippet ...
}
What do you think is the sequence of code execution? Remember that control's invoke and BeginInvoke are all executed on the main thread, the UI thread
A----->begininvokethread thread executes on the UI thread, the UI continues to execute code snippet B, and Invokethread executes the code snippet C--------------> Regardless of whether the UI has executed code snippet B, The begininvokethread thread then marshals the message to the UI and does not wait for it to proceed down-------->ui after processing the Butbegininvoke_click message, Handles messages that are marshaled by begininvokethread threads.


Explanation:
1. The UI performs a
2. The UI thread Begininvokethread,b and C execute simultaneously, B executes on the thread UI, and C executes on the thread begininvokethread.
3. Begininvokethread Marshal the message to the UI, and then continue to execute the code D,UI after processing the message, processing the message invokethread marshaling, that is, code snippet E
There is a doubt: if the UI is finished first, Is it possible to begininvokethread the message to the UI after a period of time before the UI continues to perform the marshaling message E. As shown in the light green part. The BeginInvoke of the


Control is relative to the thread that invoked it, that is, the begininvokethread is relatively asynchronous.
So we can think of. If you want to asynchronously take long time data, such as reading a large amount of data from a database, we should do so.
(1) If you want to block the calling thread, then the calling code (c), the code snippet D is deleted, and C. is changed to a lengthy operation because the operation is done in another thread. Code snippet E changes to the method of updating the interface.
(2) If you do not want to block the calling thread, then the calling code (iv), the code snippet D is deleted, and C is replaced by a lengthy operation, because the operation is done in another thread. Code snippet E changes to the method of updating the interface.

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.