locked
How to use a Dispatch Timer RRS feed

  • Question

  • How can I have a Dispatch Timer run continuously in the background and not have the app frozen? I want to still be able to use the app with the timer on. I'm a beginner, so any help? Thanks!
    Wednesday, September 18, 2013 11:32 PM

Answers

  • A DispatcherTimer will run only when the app is running, not when it is suspended. You can set it up with an Interval to control how often it should call its Tick delegate. Here's a code snippet to call the TimerTick function approximately every second:

            Dim timer As New DispatcherTimer()
            timer.Interval = TimeSpan.FromSeconds(1)
            AddHandler timer.Tick, AddressOf TimerTick
            timer.Start()

    For test purposes, TimerTick will update a counter and tell us how many times it has been called:

        Private Sub TimerTick(sender As Object, e As Object)
            numTicks = numTicks + 1
            outputField.Text = numTicks
        End Sub

    --Rob

    • Marked as answer by Young Genius Thursday, September 19, 2013 11:49 PM
    Wednesday, September 18, 2013 11:48 PM
    Moderator

All replies

  • A DispatcherTimer will run only when the app is running, not when it is suspended. You can set it up with an Interval to control how often it should call its Tick delegate. Here's a code snippet to call the TimerTick function approximately every second:

            Dim timer As New DispatcherTimer()
            timer.Interval = TimeSpan.FromSeconds(1)
            AddHandler timer.Tick, AddressOf TimerTick
            timer.Start()

    For test purposes, TimerTick will update a counter and tell us how many times it has been called:

        Private Sub TimerTick(sender As Object, e As Object)
            numTicks = numTicks + 1
            outputField.Text = numTicks
        End Sub

    --Rob

    • Marked as answer by Young Genius Thursday, September 19, 2013 11:49 PM
    Wednesday, September 18, 2013 11:48 PM
    Moderator
  • Thanks!
    Thursday, September 19, 2013 11:48 PM