The Invoke and BeginInvoke of Control are relative to the branch thread (because they are generally called in the branch thread to update the main thread ui). Invoke is immediately inserted into the main thread for execution, and BeginInvoke is executed only after the main thread ends.
Recently, the Invoke and BeginInvoke involved a huge number of tasks, so I checked some relevant information and sorted it out as follows. Thank you for understanding the true meaning of Invoke and BeginInvoke in this article.
(1) Control Invoke and BeginInvoke
Based on the following knowledge:
(1) The Invoke and BeginInvoke of Control are different from those of Delegate.
(2) The parameters of Control Invoke and BeginInvoke are delegate. The delegate method is executed on the Control thread, that is, the UI thread we usually call.
Let's look at code (1) (Control Invoke)
Private delegate void InvokeDelegate ();
Private void InvokeMethod (){
// C code segment
}
Private void butInvoke_Click (object sender, EventArgs e ){
// A code snippet .......
This. Invoke (new InvokeDelegate (InvokeMethod ));
// B code segment ......
}
What do you think is the code execution sequence? Remember that the Invoke and BeginInvoke of Control are executed on the main thread, that is, the UI thread.
A ------> C ----------------> B
Explanation: (1) After A finishes executing on the UI thread, start Invoke. Invoke is synchronous.
(2) code segment B does not execute, but immediately executes the InvokeMethod method on the UI thread, that is, code segment C.
(3) After the InvokeMethod method is executed, code segment C continues to be executed on the UI thread.
Check out the code (2) and Control BeginInvoke
Private delegate void BeginInvokeDelegate ();
Private void BeginInvokeMethod (){
// C code segment
}
Private void butBeginInvoke_Click (object sender, EventArgs e ){
// A code snippet .......
This. BeginInvoke (new BeginInvokeDelegate (BeginInvokeMethod ));
// B code segment ......
}
What do you think is the code execution sequence? Remember that the Invoke and BeginInvoke of Control are executed on the main thread, that is, the UI thread.
A -----------> B ---------------> C is cautious. This is only for reference... I'm not sure about the execution sequence. If anyone knows the sequence, please let me know.
Explanation: (1) After A finishes executing on the UI thread, BeginInvoke starts and BeginInvoke is asynchronous.
(2) InvokeMethod method, that is, code segment C is not executed, but B is executed on the UI thread immediately.
(3) After code segment B is executed (that is, after the butBeginInvoke_Click method is executed), The InvokeMethod method, that is, code segment C, continues to be executed on the UI thread.
Therefore, we know that:
The delegate methods of Control Invoke and BeginInvoke are executed on the main thread, that is, the UI thread. That is to say, if your delegate method is used to retrieve long-time data and then update the interface or something, never call Control on the UI thread. invoke and Control. beginInvoke, because these are still blocking the UI thread, causing the UI to be suspended.
So what does Asynchronization mean?
Asynchronization refers to the process of Asynchronization relative to the thread that calls BeginInvoke, rather than the UI thread. You can call BeginInvoke on the UI thread, but of course not. ---- This is a comment from "The true meanings of Invoke and BeginInvoke.
The BeginInvoke principle is to convert the called method into a message, and then call RegisterWindowMessage () in Win32 API to send a message to the UI window. ---- This is a comment from "The true meanings of Invoke and BeginInvoke.
(2) We Use Thread to call BeginInvoke and Invoke
We open a thread to let the thread execute some time-consuming operations, and then return to the user UI thread with Control. Invoke and Control. BeginInvoke to execute interface updates.
Code (3) Thread calls the Control Invoke
Private Thread invokeThread;
Private delegate void invokeDelegate ();
Private void StartMethod (){
// C code segment ......
Control. Invoke (new invokeDelegate (invokeMethod ));
// D code segment ......
}
Private void invokeMethod (){
// Ecode segment
}
Private void butInvoke_Click (object sender, EventArgs e ){
// A code snippet .......
InvokeThread = new Thread (new ThreadStart (StartMethod ));
InvokeThread. Start ();
// B code segment ......
}
What do you think is the code execution sequence? Remember that the Invoke and BeginInvoke of Control are executed on the main thread, that is, the UI thread.
A ------> (Start starts with C of B and StartMethod at the same time) ----> (C is finished, no matter whether B is finished or not, invokeThread sends the message (invoke) to the UI thread, and then wait) ----> after the UI thread finishes processing the butInvoke_Click message, it processes the message sent from the invokeThread and executes the invokeMethod method, that is, code segment E, after processing, the UI thread switches to the invokeThread thread.
This Control. Invoke is synchronized with the invokeThread thread and stops it from running.
Explanation:
1. UI execution
2. The UI open thread InvokeThread, B and C are executed simultaneously, B is executed on the thread UI, and C is executed on the thread invokeThread.
3. InvokeThread sends messages to the UI and waits for them. After the UI finishes processing the messages, it processes the messages sent by invokeThread, that is, the code segment E
4. After the UI executes E, it is transferred to the invokeThread thread. The invokeThread thread executes the code segment D.
Code (4) Thread calls the BeginInvoke of Control
Private Thread beginInvokeThread;
Private delegate void beginInvokeDelegate ();
Private void StartMethod (){
// C code segment ......
Control. BeginInvoke (new beginInvokeDelegate (beginInvokeMethod ));
// D code segment ......
}
Private void beginInvokeMethod (){
// Ecode segment
}
Private void butBeginInvoke_Click (object sender, EventArgs e ){
// A code snippet .......
Begininvokethread = new thread (New threadstart (startmethod ));
Begininvokethread. Start ();
// B code segment ......
}
What do you think is the code execution sequence? Remember that the invoke and begininvoke of control are executed on the main thread, that is, the UI thread.
A is executed on the UI thread -----> begininvokethread starts to be executed, UI continues to execute code segment B, and invokethread concurrently executes code segment C --------------> no matter whether the UI has completed executing code segment B, at this time, the begininvokethread thread sends the message to the UI, and does not wait for it. Continue to run --------> after the UI finishes processing the butbegininvoke_click message, it processes the message sent by the begininvokethread thread.
Explanation:
1. Ui execution
2. The UI open thread begininvokethread, B and C are executed simultaneously, B is executed on the thread UI, and C is executed on the begininvokethread thread.
3. Begininvokethread: sends a message to the UI, and then executes code D. After the UI finishes processing the message, it processes the message sent by invokethread, that is, code segment E.
A bit of doubt: if the UI is executed first, is it possible that the message will be sent to the UI only after a period of begininvokethread, and then the UI will continue to execute the message e. The light green part.
The begininvoke of control is relative to the thread that calls it, that is, begininvokethread is asynchronous.
Therefore, we can think of it. To asynchronously retrieve long-time data, such as reading a large amount of data from the database, we should do this.
(1) If you want to block the calling thread, call code (3), delete code segment D, and change C to a time-consuming operation, this operation is performed in another thread. Code snippet E is used to update the interface.
(2) If you do not want to block the calling thread, call code (4), delete code segment D, and change C to a time-consuming operation, this operation is performed in another thread. Code snippet E is used to update the interface.
Knowledge: 1. The true meanings of Invoke and BeginInvoke