WPF实现Winform的DoEvent事件

时间:2014-11-28 22:36:58   收藏:0   阅读:1799

WPF实现Winform的DoEvent事件

 

bubuko.com,布布扣
 1  C#中的DoEvent事件可以实现消息处理的一些特殊操作,可以做出延迟响应的事件。
 2   C# code如下:
 3 
 4       int tick=Environment.TickCount;
 5              while (Environment.TickCount-tick<2000)
 6              {
 7                  Application.DoEvents();
 8              }
 9              timer1.Stop();
10 
11  
12 
13   延迟两秒响应鼠标事件或点击事件。
14 
15   在WPF中没有DoEvent事件,可以用Dispatcher来实现相关的功能。(涉及多线程,不再详述。)
16 
17          /// <summary>
18          /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
19         /// </summary>
20          public void DoEvent()
21          {
22              DispatcherFrame frame = new DispatcherFrame();
23              Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
24              Dispatcher.PushFrame(frame);
25          }
26           public object ExitFrame(object f)
27           {
28               ((DispatcherFrame)f).Continue = false;
29               return null;
30           }
31          //***********************************************
View Code

 

 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!