您的位置:首页 > 其它

WPF实现Winform的DoEvent事件

2016-09-17 02:00 232 查看
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;
}
//***********************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: