Forum Xamarin Xamarin.Forms

Simple Timer

Hi Does any one have and example of a simple timer that shows the elapsed time every 1ms?
I can only use stopwatch but stop watch will only update the label when I stop it and I can't use timer for some reason.


  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    will only update the label when I stop it

    Then your binding is wonky.

    No timer or stopwatch required.
    Every 1 second update a property

            async void Heartbeat()
                while (true)
                    await Task.Delay(1000);
                    DeviceDateTime = DateTime.Now;

    Then just bind to the DeviceDateTime property

  • MnemonicMnemonic USMember

    @ClintStLaurent I'm a little lost how I bind it to DeviceDateTime?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    DeviceDateTime is a property on one of your ViewModels... or even at app level. You would bind to it like any other property in your ViewModels. You are building along an MVVM pattern, aren't you?
    If you're not familiar with databinding that's something you need to stop and get familiar with first. All XAML software (WPF or Xamarin) is assuming you're doing MVVM. Feel free to work through this series to give yourself an overview. It shouldn't take more than an hour. - the reading order is on the right.

  • JohnHardmanJohnHardman GBUniversity admin
    edited October 2017

    When using timers, take care not to introduce timer-drift (unless you don't need the timer to be accurate), and be aware (which you have probably already seen) that updating the display using a 1 second delay is liable to produce a stutter in the display.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Good points. Which is why I don't use timers. Task.Delay works pretty reliably. I put in one heartbeat pulse for app-wide use. This way every other mechanism keys off the same pulse and remains in sync. I've always hated seeing 3 clocks all 'tick' slightly out of time from each other because they all used their own timer.

Sign In or Register to comment.