I need to know whether Control.BeginInvoke and Control.Invoke calls will execute in the order they are called.
I have the following scenario:
- UI thread is blocked
- WCF thread calls Control.BeginInvoke
- WCF thread calls Control.Invoke (or possibly BeginInvoke again)
- UI thread is unblocked
- ??
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.
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.