Win 2k8 SP2 中 Stop() 后 DispatcherTimer 持续触发
今天我收到了一份有趣的错误报告。
我有一个 DispatcherTimer,其 Tick 调用包含 Stop() 方法调用的 EventHandler。这会停止我们在开发中使用的平台(Windows XP SP3 和 Windows 7)上的计时器,但在 Windows Server 2008 SP2 计算机上运行时计时器似乎不会停止。
这是一个 .NET 3.5 项目。
我想知道这是否是 Win 2k8 中 System.Windows.Threading 中的一个已知错误,或者我是否在代码中做错了什么。
代码的相关部分如下:
public DispatcherTimer UserDelayTimer;
private void _HierTreeControlWPF_Loaded(object sender, RoutedEventArgs e)
{
UserDelayTimer = new DispatcherTimer();
UserDelayTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //500 ms
UserDelayTimer.Tick += new EventHandler(OnTimerEvent);
UserDelayTimer.Start();
}
/// <summary>
/// Timer to run update after the user has stopped making selections in the hierarchy view.
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
void OnTimerEvent(object sender, EventArgs e)
{
if (HierTreeAfterCheck_Event != null && !HierTreeCheckEvent_Suppressed)
HierTreeAfterCheck_Event();
UserDelayTimer.Stop();
}
//This method is run whenever the mouse moves or makes a selection in the hierarchy tree.
//The idea is that HierTreeAfterCheck_Event() will only run after the user has stopped making
//selections for a certain amount of time.
public void ResetUserDelayTimer(object sender, MouseButtonEventArgs e)
{
if (UserDelayTimer.IsEnabled) //if the timer is running, restart it to buy more time.
{
UserDelayTimer.Stop();
UserDelayTimer.Start();
}
}
非常感谢!
I got an interesting bug report today.
I have a DispatcherTimer whose Tick calls an EventHandler which contains a Stop() method call. This stops the timer on the platforms we use in development (Windows XP SP3 and Windows 7), but the timer does not seem to stop when run on a Windows Server 2008 SP2 machine.
This is a .NET 3.5 project.
I am wondering if this is a known bug in System.Windows.Threading in Win 2k8 or if I am doing something wrong in my code.
The relevant parts of the code are below:
public DispatcherTimer UserDelayTimer;
private void _HierTreeControlWPF_Loaded(object sender, RoutedEventArgs e)
{
UserDelayTimer = new DispatcherTimer();
UserDelayTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //500 ms
UserDelayTimer.Tick += new EventHandler(OnTimerEvent);
UserDelayTimer.Start();
}
/// <summary>
/// Timer to run update after the user has stopped making selections in the hierarchy view.
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
void OnTimerEvent(object sender, EventArgs e)
{
if (HierTreeAfterCheck_Event != null && !HierTreeCheckEvent_Suppressed)
HierTreeAfterCheck_Event();
UserDelayTimer.Stop();
}
//This method is run whenever the mouse moves or makes a selection in the hierarchy tree.
//The idea is that HierTreeAfterCheck_Event() will only run after the user has stopped making
//selections for a certain amount of time.
public void ResetUserDelayTimer(object sender, MouseButtonEventArgs e)
{
if (UserDelayTimer.IsEnabled) //if the timer is running, restart it to buy more time.
{
UserDelayTimer.Stop();
UserDelayTimer.Start();
}
}
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了。结果我必须修改 OnTimerEvent 的发送者,而不是计时器本身的公共实例。
Figured it out. Turns out I had to modify the sender of OnTimerEvent, instead of the public instance of the timer itself.