Recently, the control of the invoke and BeginInvoke engaged in the head of the big, on the search for some relevant information, collation as follows.
The control's invoke and BeginInvoke are executed relative to the spur thread (because it is typically called in a spur thread, to update the main threaded UI), and invoke is immediately inserted into the main thread, while BeginInvoke is executed when the main thread ends.
(i) control's Invoke and BeginInvoke
We should be based on the following understanding:
(1) The Invoke and BeginInvoke ofControl are different from Delegate 's Invoke and BeginInvoke.
(2) The control's invoke and BeginInvoke parameters are delegate, the delegate method is executed on the control thread, which is what we call the UI thread .
We look at the code (a) (the control's Invoke)
Private delegate void Invokedelegate ();p rivate 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 order of execution of the code? Remember that control's invoke and BeginInvoke are executed on the main thread, the UI thread
A------>c---------------->b
Explanation: (1) A after executing on the UI thread, start Invoke,invoke is synchronous
(2) Code snippet B does not execute, but immediately executes the InvokeMethod method on the UI thread, which is code snippet C.
(3) After the InvokeMethod method executes, code snippet C continues to execute on the UI thread.
Look at the Code (ii), the BeginInvoke of control
Private delegate void Begininvokedelegate ();p rivate 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 order of execution of the code? Remember that both the invoke and BeginInvoke of control are executed on the main thread, the UI thread,
A----------->b--------------->c cautious, This is only a reference ..... , I am not sure of the order of execution, if anyone knows, please let us know.
Explanation: (1) A after executing on the UI thread, start Begininvoke,begininvoke is asynchronous
(2) InvokeMethod method, that is, code snippet C does not execute, but immediately executes code snippet b on the UI thread. After the
(3) code snippet B finishes executing (that is, after the Butbegininvoke_click method executes), the InvokeMethod method, that is, code snippet C, continues execution on the UI thread.
Thus, we know that the delegate method of
control's Invoke and BeginInvoke is performed on the main thread, which is the UI thread. In other words, if your delegate method is used to take long-time data, and then update the interface, do not call Control.Invoke and Control.BeginInvoke on the UI thread, because these are still blocking the UI thread, causing the interface to feign animation.
So what does this asynchronous mean?
Async refers to a thread that is asynchronous relative to the calling BeginInvoke, not to the UI thread, and you call BeginInvoke on the UI thread, of course not. ---excerpt from the comments in the article "Invoke and BeginInvoke's true meaning". The principle of
BeginInvoke is to marshal the called method into a message and then call RegisterWindowMessage () in the Win32 API to send a message to the UI window. ---excerpt from the comments in the article "Invoke and BeginInvoke's true meaning".
(b) We use thread to invoke BeginInvoke and invoke
we open a thread to allow the thread to perform some time-consuming operations, Then use Control.Invoke and Control.BeginInvoke back to the user UI thread to perform the interface update.
Code (c) thread invoke control's invoke
Private Thread invokethread;private delegate void Invokedelegate ();p rivate 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 of the order in which the code is executed? Remember that both the invoke and BeginInvoke of control are executed on the main thread, the UI thread
a------> (start B and Startmethod C are executed simultaneously)-- --(c executes, regardless of whether B is finished, invokethread the message to the UI thread, and then waits for itself)---->UI thread finishes processing This control.invoke is synchronized relative to the invokethread thread and prevents it from running.
Explanation:
1. The UI executes a
2. UI Threads Invokethread,b and C execute concurrently, B executes on the thread UI, and C executes on thread invokethread.
3. Invokethread Marshals the message to the UI, and then waits for itself, after the UI finishes processing the message, processing the invokethread marshaled message, which is code snippet E
4. After the UI finishes executing E, go to thread invokethread, Invokethread thread executes code snippet D
Code (iv) thread call control BeginInvoke
Private Thread begininvokethread;private delegate void Begininvokedelegate ();p rivate 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 order of execution of the code? Remember that control's invoke and BeginInvoke are executed on the main thread, the UI thread
A-----The >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 finished executing code snippet B, The begininvokethread thread then marshals the message to the UI, does not wait for itself, continues down-------->ui finishes processing the Butbegininvoke_click message, Handles the message marshaled by the Begininvokethread thread.
Explain:
1. UI execution A
2. UI Threads Begininvokethread,b and C execute concurrently, B executes on the thread UI, and C executes on thread begininvokethread.
3. Begininvokethread Marshals the message to the UI, and then proceeds to execute the code itself D,UI processing the message, processing the invokethread marshaling message, the code snippet E
A bit of a question: If the UI executes first, is it possible that the message is begininvokethread to the UI after a period of time, and then the UI continues to execute the marshaled message E. A light green part.
The BeginInvoke of control is relative to the thread that called it, that is, Begininvokethread is relatively asynchronous.
So we can think of. If you want to take long-time data asynchronously, such as reading large amounts of data from a database, we should do so.
(1) If you want to block the calling thread, then the calling code (iii), code snippet D is deleted, and C changes to a lengthy operation because the operation is done in another thread. Change the code snippet E to the method of updating the interface.
(2) If you do not want to block the calling thread, then call code (iv), code snippet D to delete, C to take a long time operation, because this operation is done in another thread. Change the code snippet E to the method of updating the interface.
The Invoke and BeginInvoke of control in C # are relative to the spur thread