C#中的DoEvent事件可以实现消息处理的一些特殊操作,可以做出延迟响应的事件。
C# code如下:
int tick=Environment.TickCount;
while (Environment.TickCount-tick<2000)
{
Application.DoEvents();
}
timer1.Stop();
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;
}
//***********************************************
/// 模仿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;
}
//***********************************************
文章末尾固定信息

我的微信
我的微信
一个码农、工程狮、集能量和智慧于一身的、DIY高手、小伙伴er很多的、80后奶爸。
评论