Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will multiple Control.BeginInvoke/Invoke calls execute in order?

I need to know whether Control.BeginInvoke and Control.Invoke calls will execute in the order they are called.

I have the following scenario:

  1. UI thread is blocked
  2. WCF thread calls Control.BeginInvoke
  3. WCF thread calls Control.Invoke (or possibly BeginInvoke again)
  4. UI thread is unblocked
  5. ??

The execution order of step 1-4 is guaranteed to be in the shown order (technically the order is not guaranteed to be that way, but the question I have is only relevant if the order is as shown).

The question I have is whether there is any chance that the Invoke/BeginInvoke call in step 3 is executed before the BeginInvoke call in step 2?

Also, please don't comment on blocking the UI thread.

like image 732
cornergraf Avatar asked Dec 10 '09 13:12

cornergraf


2 Answers

In your case, step 2 will always execute before step 3. BeginInvoke on the UI thread will execute in the order it has been queued.

The UI thread is in fact a message pump, it has a single message queue with only one thread consuming it, so it's guaranteed that work items will be executed in the order they were queued.

It's with Delegate.BeginInvoke that the order of execution may be non-sequential.

like image 67
Jeff Cyr Avatar answered Nov 17 '22 00:11

Jeff Cyr


BeginInvoke calls are queued on the destination thread (as they are posted in order of arrival).

Synchronous calls on the WCF Thread (step 3) may be executed before asynchronous calls (step 2) made from this thread.

like image 33
Laurent Etiemble Avatar answered Nov 17 '22 01:11

Laurent Etiemble