1: private static DispatcherOperationCallback exitFrameCallback = new
2: DispatcherOperationCallback(ExitFrame);
3:
4: /// <summary>
5: /// Processes all UI messages currently in the message queue.
6: /// </summary>
7: public static void DoEvents()
8: {
9: // Create new nested message pump.
10: DispatcherFrame nestedFrame = new DispatcherFrame();
11:
12: // Dispatch a callback to the current message queue, when getting called,
13: // this callback will end the nested message loop.
14: // note that the priority of this callback should be lower than the that
15: // of UI event messages.
16: DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
17: DispatcherPriority.Background, exitFrameCallback, nestedFrame);
18:
19: // pump the nested message loop, the nested message loop will immediately
20: // process the messages left inside the message queue.
21: Dispatcher.PushFrame(nestedFrame);
22:
23: // If the "exitFrame" callback doesn't get finished, Abort it.
24: if (exitOperation.Status != DispatcherOperationStatus.Completed)
25: {
26: exitOperation.Abort();
27: }
28: }
29:
30: private static Object ExitFrame(Object state)
31: {
32: DispatcherFrame frame = state as DispatcherFrame;
33:
34: // Exit the nested message loop.
35: frame.Continue = false;
36: return null;
37: }