Win 2k8 SP2 中 Stop() 后 DispatcherTimer 持续触发

发布于 2024-11-18 22:23:00 字数 1552 浏览 2 评论 0原文

今天我收到了一份有趣的错误报告。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-25 22:23:00

想通了。结果我必须修改 OnTimerEvent 的发送者,而不是计时器本身的公共实例。

Figured it out. Turns out I had to modify the sender of OnTimerEvent, instead of the public instance of the timer itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文