andrew4582

DoEvents for WPF

Mar 5th, 2011
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
  2.        
  3.         public static void DoEvents() {
  4.             // Create new nested message pump.
  5.             DispatcherFrame nestedFrame = new DispatcherFrame();
  6.  
  7.             // Dispatch a callback to the current message queue, when getting called,
  8.             // this callback will end the nested message loop.
  9.             // note that the priority of this callback should be lower than the that of UI event messages.
  10.             DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
  11.                 DispatcherPriority.Background,exitFrameCallback,nestedFrame);
  12.  
  13.             // pump the nested message loop, the nested message loop will
  14.             // immediately process the messages left inside the message queue.
  15.             Dispatcher.PushFrame(nestedFrame);
  16.  
  17.             // If the "exitFrame" callback doesn't get finished, Abort it.
  18.             if(exitOperation.Status != DispatcherOperationStatus.Completed) {
  19.                 exitOperation.Abort();
  20.             }
  21.         }
  22.  
  23.         // Exit the nested message loop.
  24.         private static Object ExitFrame(Object state) {
  25.             DispatcherFrame frame = state as DispatcherFrame;
  26.             frame.Continue = false;
  27.             return null;
  28.         }
Advertisement
Add Comment
Please, Sign In to add comment