WPF实现C#中DoEvent事件(可实现延迟响应事件)

来源:互联网 发布:网络销售股票怎么赚钱 编辑:程序博客网 时间:2024/05/20 07:33
C#中的DoEvent事件可以实现消息处理的一些特殊操作,可以做出延迟响应的事件。
C# code如下:
    int tick=Environment.TickCount;
            while (Environment.TickCount-tick<2000)
            {
                Application.DoEvents();
            }
            timer1.Stop();
延迟两秒响应鼠标事件或点击事件。
在WPF中没有DoEvent事件,可以用Dispatcher来实现相关的功能。(涉及多线程,不再详述。)
   /// <summary>
        /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
        /// </summary>
        public void DoEvent()
        {
            DispatcherFrame frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }
         public object ExitFrame(object f)
         {
             ((DispatcherFrame)f).Continue = false;
             return null;
         }
0 0
原创粉丝点击