Skip to main content

 none
[UWP] DispatcherTimer accuracy problem: not firing when Interval greater than 0.45+seconds RRS feed

  • Question

  • I'm working on my UWP app, and I  am using DispatcherTimer because that part need to update UI.

    And the Interval of the DispatcherTimer is changing in my app(from 0~3s)

    But when the Interval > 0.45 (more or less), the Tick seems can not be triggered.

    It works well when Interval < 0.4.

    The code is like:

    void someFunc() {
        dispatcherTimer.Interval = new TimerSpan(ticks);
        dispatcherTimer.Start();
    }
    
    void dispatcherTimer_Tick(object sender, object e) {
        doSomethingWithUI();
        dispatcherTimer.Stop();
    }

    I am using a thread to solve this problem, but it's accuracy is not great and has some other problems.

    So what's going on with this DispatcherTimer?

    Appreciate!

    • Moved by Hart Wang Wednesday, March 28, 2018 7:44 AM
    Tuesday, March 27, 2018 10:07 AM

All replies

  • Hi,

    Thank you for posting here.

    Since your issue is related to uwp forum, I will move the case to uwp forum.

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 28, 2018 7:44 AM
  • Hi CuberMessenger,

    May I know your OS version and project target version?

    I made a test for this and the tick is fired when I set the interval as 450 milliseconds.

    Here is my code:

       

      public DispatcherTimer dispatcherTimer;
            public MainPage()
            {
                this.InitializeComponent();
                dispatcherTimer = new DispatcherTimer();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                someFunc();
            }
    
            void someFunc()
            {
                dispatcherTimer.Interval = TimeSpan.FromMilliseconds(450);
                dispatcherTimer.Tick += dispatcherTimer_Tick;
                dispatcherTimer.Start();
            }
    
            void dispatcherTimer_Tick(object sender, object e)
            {
                Debug.WriteLine(DateTime.Now);
    
                dispatcherTimer.Stop();
            }

    As you could see, I’m using TimeSpan.FromMilliseconds(450) to set the interval which means 0.45 second.

    You could try my code and test again.

    Best regards

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 29, 2018 4:22 AM
    Moderator
  • Thanks.
    Friday, March 30, 2018 2:49 AM
  • The real condition is a little more complicate, my OS version is 16299.306 and the project target version is 16299(also the min version)

    The outcome of my code is:

        Interval < 0.45(more or less) -> Fired as wished

        Interval >=0.45(more or less) -> seems never going to fire or fire after 10+ seconds

    So I guess is the problem is the way DIspatcherTimer works.

    I searched some related questions about DispatcherTimer and I found it's not an accuracy timer because it works in a dispatcher queue or whatever.(maybe I'm wrong)

    And I have some other DispatcherTimer running while this part of code is perform, but the .NET of UWP seems did not provide the access of the priority of DispatcherTimer, so I stuck on here.

    The "someFunc" is called when "space" KeyDown called.

    My original code is below, the function is detect is user holding the space_key for a enough specific time.

    private void StartHoldingTick() {
    
                IsHolding = true;
    
                TimerTextBlock.Foreground = YellowBrush;
                
                HoldingCheckTimer.Interval = new TimeSpan(appSettings.StartDelay);
    
                HoldingCheckTimer.Start();
    }
    
    private void HoldingCheckTimer_Tick(object sender, object e) {
    
                if (IsHolding) {
    
                    TimerStatus = TimerStatus.Holding;
    
                    TimerTextBlock.Foreground = GreenBrush;
    
                }
    
                IsHolding = false;
    
                HoldingCheckTimer.Stop();
    }
    Thanks for reply me.


    Friday, March 30, 2018 3:06 AM
  • Hello,

    Probably, that problem is due to unexpected KeyDown events. As you know, keyboard has auto-repeat function, which fires KeyDown event repeatedly while holding the key. Since continual calls of StartHoldingTick() in your KeyDown event handler keep on restarting the timer again and again, the timer won't tick.

    Setting a flag on the first KeyDown event, try ignoring subsequent events.

        private bool IsActuallyHolding = false;
    
        private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if ((e.Key == Windows.System.VirtualKey.Space) && (!IsActuallyHolding))
            {
                IsActuallyHolding = true;
                StartHoldingTick();
            }
        }
        private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Space)
            {
                IsActuallyHolding = false;
            }
        }
    • Edited by FEC-4RP Friday, March 30, 2018 7:15 AM
    Friday, March 30, 2018 6:45 AM
  • Hi,

    Any updates?

    Have you solved your problem?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 3, 2018 8:43 AM
    Moderator