MicrosoftDocs / feedback

📢 docs.microsoft.com site feedback
https://learn.microsoft.com
Creative Commons Attribution 4.0 International
239 stars 160 forks source link

DispatcherTimer.Start() - Start resets the timer interval #1723

Closed ghost closed 4 years ago

ghost commented 5 years ago

DispatcherTimer.Start() does not reset the timer interval if running.

To reset a running DispatcherTimer interval requires either setting IsEnabled false or calling Stop() before calling Start() to reset the interval.

welcome[bot] commented 5 years ago

Thank you for creating the issue! One of our team members will get back to you shortly with additional information. If this is a product issue, please close this and contact the particular product's support instead (see https://support.microsoft.com/allproducts for the list of support websites).

ryanmajidi commented 5 years ago

@GrantMeStrength Can you please take a look at this issue?

GrantMeStrength commented 4 years ago

I tested the timer with the following code. I found that the timer does restart if Start is called. It's possible you are creating a new timer rather than calling Start on an existing timer.

public sealed partial class MainPage : Page {

    DispatcherTimer dispatcherTimer;

    public MainPage()
    {
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 5);

        this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        mytext.Text = "Button clicked, timer start"; // Every time this is called, another 5 seconds is added.
        dispatcherTimer.Start();
    }

    void dispatcherTimer_Tick(object sender, object e)
    {
        mytext.Text = "TICK";
    }
    }

}

ghost commented 4 years ago

Thanks for looking into this John. Back in July 2019 the problem was real, now I don't even know if it was the same Timer? Must have been a problem with the DeLorean... Cheers

mrhodel commented 2 years ago

This is a problem. I'm using .net 4.8 and a stop is required to reset the interval. Contrary to what the documentation says, Start() does not restart the timer.

seba30 commented 1 year ago

I faced the same problem with NET48. I created a timer (with TimeSpan.FromMilliseconds(100)) and call Start on every interrupt of my routine. I also added some Console.WriteLine. The outputs are:

2023-01-28T21:29:58.1773610+01:00: Timer start 1528 2023-01-28T21:29:58.1853053+01:00: Timer start 1528 2023-01-28T21:29:58.1888907+01:00: Timer start 1528 2023-01-28T21:29:58.1919945+01:00: Timer start 1528 2023-01-28T21:29:58.2203149+01:00: Timer start 1528 2023-01-28T21:29:58.2232329+01:00: Timer start 1528 2023-01-28T21:29:58.2262691+01:00: Timer start 1528 2023-01-28T21:29:58.2282532+01:00: Timer start 1528 2023-01-28T21:29:58.2548373+01:00: Timer start 1528 2023-01-28T21:29:58.2608372+01:00: Timer start 1528 2023-01-28T21:29:58.2618571+01:00: Timer start 1528 2023-01-28T21:29:58.2658600+01:00: Timer start 1528 2023-01-28T21:29:58.2688371+01:00: Timer start 1528 2023-01-28T21:29:58.2758410+01:00: Timer done 1528 2023-01-28T21:29:58.2758410+01:00: Timer start 1528 2023-01-28T21:29:58.2803360+01:00: Timer start 1528 2023-01-28T21:29:58.2823150+01:00: Timer start 1528 2023-01-28T21:29:58.3085252+01:00: Timer start 1528 2023-01-28T21:29:58.3144913+01:00: Timer start 1528 2023-01-28T21:29:58.3174925+01:00: Timer start 1528 2023-01-28T21:29:58.3204922+01:00: Timer start 1528 2023-01-28T21:29:58.3244917+01:00: Timer start 1528 2023-01-28T21:29:58.3274919+01:00: Timer start 1528 2023-01-28T21:29:58.3304905+01:00: Timer start 1528 2023-01-28T21:29:58.3324905+01:00: Timer start 1528 2023-01-28T21:29:58.3597377+01:00: Timer start 1528 2023-01-28T21:29:58.3611107+01:00: Timer start 1528 2023-01-28T21:29:58.3631703+01:00: Timer start 1528 2023-01-28T21:29:58.3661776+01:00: Timer start 1528 2023-01-28T21:29:58.3834139+01:00: Timer done 1528 2023-01-28T21:29:58.4093558+01:00: Timer start 1528 2023-01-28T21:29:58.4133555+01:00: Timer start 1528 2023-01-28T21:29:58.4153356+01:00: Timer start 1528 2023-01-28T21:29:58.4173590+01:00: Timer start 1528 2023-01-28T21:29:58.5163189+01:00: Timer done 1528

As you can see, the last Start call is less than 100 ms before the EventHandler is raised ("Timer done").

HeyFlash commented 1 year ago

I can confirm that this is the case on .net 4.7.2. Contrary to documentation the timer does not restart. No new timer is being created. Calling .Stop() Immediately before .Start resolves the issue.