Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Jul 13, 2019 · 7 comments
Closed

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

ghost opened this issue Jul 13, 2019 · 7 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Jul 13, 2019

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
Copy link

welcome bot commented Jul 13, 2019

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
Copy link
Collaborator

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

@PRMerger8 PRMerger8 added the Pri3 label Apr 2, 2020
@GrantMeStrength
Copy link
Collaborator

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
Copy link
Author

ghost commented Aug 30, 2020

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
Copy link

mrhodel commented Jul 28, 2022

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
Copy link

seba30 commented Jan 28, 2023

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants