Control.Dispatcher.BeginInvoke () and Control.Dispatcher.Invoke () is order execution confusing?

I am using WPF and I was confused by the execution order of Control.Dispatcher.BeginInvoke() and Control.Dispatcher.Invoke() .

I will show an example code below

 backgroundThread = new Thread(BackgroundThread); backgroundThread.Start(); public void BackgroundThread() { this.Dispatcher.BeginInvoke(new Action(delegate() { WriteLog("Run command 1"); })); this.Dispatcher.Invoke(new Action(delegate() { WriteLog("Run command 2"); })); } 

I expect that “Command 1” will start and end before “Team 2,” but sometimes it seems that “Command 2” works before “Command 1”. I have researched a lot on the Internet and the MSDN document, but I do not understand why this is happening.

Someone please tell me the rule of these functions for sure?

Thank you very much!

T & T

+6
source share
4 answers

BeginInvoke invokes an Action , which you pass to it asynchronously in the thread that is associated with Dispatcher , while Invoke invokes this action synchronously.

In other words, Invoke immediately executes what ever the Action passed to it, and BeginInvoke puts the action that you pass it to the Dispatcher queue, which looks like a list of things Dispatcher will do, but without any guarantees when this will happen or how only the dispatcher will finish doing other things waiting in this queue.

Therefore, sometimes Dispatcher may be busy with something else and passes the action that you pass to BeginInvoke at the end of the queue until it can perform it, and then it will perform any action that you pass to Invoke , and this will cause differences in orders.

+6
source

Someone please tell me the rule of these functions for sure

Below is the diffraction

 Dispatcher.Invoke: Executes the specified delegate synchronously on the thread the Dispatcher is associated with Dispatcher.BeginInvoke: Executes the specified delegate asynchronously on the thread the Dispatcher is associated with.. 

and now your understanding

I expect that “Command 1” will start and end before “command 2”, but sometimes it seems that “Command 2” works before “Command 1”

Since your first call is asynchronous, it can end before or after the second call (which is synchronous).

+1
source

I expect that “Command 1” will start and end before “Team 2,” but sometimes it seems that “Command 2” works before “Command 1”.

Your assumption is wrong because Control.BeginInvoke works asynchronously while Control.Invoke works synchronously.

If you want command 1 to run before command 2, run command 1 with Invoke .

When you use BeginInvoke , you do not receive order guarantee , because it is up to the Dispatcher when it is going to execute it.

+1
source

Well, I think the sniffer answer is pretty clear. basiclly you get almost the whole idea here, the last thing that may be required for you to outline these concepts.

1, invoked by Invoke , which you call, will switch the current execution of the main thread to the user interface thread synchronously , of course, if it works with loooong, it will block the main one until the code execution is completed; 2. The difference of BeginInvoke is that this switch happens asynchronously , does not affect the main thread.

and I assume that you already understand why the output sequence

+1
source

Source: https://habr.com/ru/post/950955/


All Articles