C # invoke and begininvoke

Source: Internet
Author: User

From http://www.cnblogs.com/c2303191/articles/826571.html

Control. Invoke method (delegate):InYesOn the thread of the basic window handle of this controlRunThe specified delegate.

Control. begininvoke method (delegate): InCreateOn the thread where the basic handle of the control is locatedAsynchronous executionSpecify the delegate.

(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.

 

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.