开发者

DispatcherTimer Keeps Firing After Stop() in Win 2k8 SP2

开发者 https://www.devze.com 2023-03-18 20:14 出处:网络
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 (Windo

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开发者_如何学JAVA)
{
    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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消