Synchronous and asynchronous use of thread Delegation

Source: Internet
Author: User

Synchronous and asynchronous use of thread Delegation

 

The following is an explanation of invoke:

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.

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.

We useCode(1) view (control invoke)
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  Invokedelegate ();
Private Void Invokemethod (){
// C code segment
}
Private Void Butinvoke_click ( Object Sender, eventargs e ){
// A code segment .......
This . Invoke ( New Invokedelegate (invokemethod ));
// B code segment ......
}

 

 

 

  Private     Delegate     Void  Begininvokedelegate ();
Private Void Begininvokemethod (){
// C code segment
}
Private Void Butbegininvoke_click ( Object Sender, eventargs e ){
// A code segment .......
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 ---------------> CIt looks like this. I'm not sure about the execution order. If anyone knows this, please let me know.
Explanation:

(1) After executing a on the UI thread, start begininvoke. 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 segment .......
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 B andStartmethodCExecute at the same time) ----> (CAfter the execution is complete, no matter whether B has completed the execution, invokethread will send the message to the UI thread (invoke) and then wait for it) ----> after the UI thread finishes processingButinvoke_click

After the message is sent, process the message sent from invokethread and execute 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

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 execute --------> after the UI is processedButbegininvoke_click

After the message is sent, process the messages sent from the ininvokethread 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.

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.