WPF的DoEvents

2720阅读 0评论2016-01-15 dyli2000
分类:C#/.net


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Threading;

  7. namespace DrawWpf
  8. {
  9.     class WpfApplication : Application
  10.     {
  11.         private static DispatcherOperationCallback mExitFrameCallback = new DispatcherOperationCallback(ExitFrame);

  12.         private static Object ExitFrame(Object state)
  13.         {
  14.             DispatcherFrame frame = state as DispatcherFrame;
  15.             // Exit the nested message loop.
  16.             frame.Continue = false;
  17.             return null;
  18.         }

  19.         ///
  20.         /// Processes all UI messages currently in the message queue.
  21.         ///
  22.         public static void DoEvents()
  23.         {
  24.             // Create new nested message pump.
  25.             DispatcherFrame nestedFrame = new DispatcherFrame();

  26.             // Dispatch a callback to the current message queue, when getting called,
  27.             // this callback will end the nested message loop.
  28.             // note that the priority of this callback should be lower than the that of UI event messages.
  29.             DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
  30.                                                   DispatcherPriority.Background, mExitFrameCallback, nestedFrame);

  31.             // pump the nested message loop, the nested message loop will
  32.             // immediately process the messages left inside the message queue.
  33.             Dispatcher.PushFrame(nestedFrame);

  34.             // If the "exitFrame" callback doesn't get finished, Abort it.
  35.             if (exitOperation.Status != DispatcherOperationStatus.Completed)
  36.                 exitOperation.Abort();
  37.         }
  38.     }
  39. }

用法:

WpfApplication.DoEvents();

修改并转自博客:

http://blog.csdn.net/limlimlim/article/details/7754553
上一篇:WPF datagrid应用
下一篇:Error:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序