Thisen
Thisen

Reputation: 183

Making a timer in WPF

I'm making a Jungle timer program for League of Legends, where I will, when I click a button, start a timer for how long it will take the jungle camp to respawn.

My question is, how do I make such a timer?

Upvotes: 0

Views: 7003

Answers (3)

Shannon Fonseka
Shannon Fonseka

Reputation: 17

From the answer from here.

You have to use the DispatcherTime class. For that, import the following class at the very beginning of your document.

Imports System.Windows.Threading 

Then after the Class MainWindow, declare the DispatchTimer with an identifier (tmr1):

Dim tmr1 As New DispatcherTimer 

Then add this code after the declaration:

Sub New()
InitializeComponent()
tmr1 .Interval = Timespan.FromSeconds(6) 
AddHandler tmr1.Tick, AddressOf tmr1Tick
tmr1 .Start() 
End Sub 
 
Private Sub tmr1Tick(sender As System.Object, e As System.Windows.RoutedEventArgs)
'If errors exist, then type the "Private Sub tmr1Tick...End Sub"
'Enter your code. This code is for when the timer ticks
End Sub

Upvotes: 1

Code0987
Code0987

Reputation: 2618

You can use DispatcherTimer, like this

    ' Create a new DispatcherTimer
    Private _mytimer As New DispatcherTimer 

    Sub New()
        InitializeComponent()

        ' Set interval for timer
        _mytimer.Interval = TimeSpan.FromMilliseconds(10000)
        ' Handle tick event
        AddHandler _mytimer.Tick, ...

    End Sub

    Private Sub Button_Timer_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        ' Start timer on button click
        _mytimer.Start()
    End Sub

Upvotes: 3

Benny Jobigan
Benny Jobigan

Reputation: 5305

I would use Timer or perhaps DispatcherTimer. Create a textbox or whatever for the time span entry. .NET's TimeSpan class has a constructor or static helper method which can parse a string in the form hh:mm:ss.ss. When the button is pressed, create the timer using that time span (converting it to milliseconds if needed), and provide the timer a delegate/callback method. The timer will call that method when the time span has elapsed. Put some code in that method to alert you.

Upvotes: 1

Related Questions