Users of Windows Forms will know that there are three timer objects you can use, namely the Windows Forms Timer, the System Timers Timer and finally the System Threading Timer, as the following example demonstrates;
Note: this tutorial is in both C# and Visual Basic, so look beneath the Visual Basic examples for the C# Version.
Visual Basic
Public Class Form1
Dim timerCallBack As System.Threading.TimerCallback
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
”Winforms Timer
Dim wfTimer As System.Windows.Forms.Timer()
”Timers Timer
Dim tTimer As System.Timers.Timer()
”Threading Timer
timerCallBack = AddressOf PrintTime
Dim thTimer As System.Threading.Timer = New System.Threading.Timer(Me.timerCallBack)
End Sub
Private Sub PrintTime(ByVal obj As System.Object)
”Print the time to screen
End Sub
End Class
C#
using System.Windows.Forms;
namespace TimersExample
{
public partial class Form1 : Form
{
System.Threading.TimerCallback timerCallBack;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Winforms Timer
System.Windows.Forms.Timer wfTimer = new Timer();
//Timers Timer
System.Timers.Timer tTimer = new System.Timers.Timer();
//Threading Timer
timerCallBack = new System.Threading.TimerCallback(PrintTime);
System.Threading.Timer thTimer = new System.Threading.Timer(this.timerCallBack);
}
private void PrintTime(object obj)
{
//Print the time to screen
}
}
}
In both WPF and Silverlight there is a new timer (the fourth .NET framework timer) called the System.Windows.Threading.DispatcherTimer. To demonstrate how this works, look at the following simple code sample. In it, there is some XAML that declares a label, button and a button click and window loaded events (In Silverlight use the UserControl_Loaded event for the page instead of Window_Loaded).
<Window x:Class="WpfTimer.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<StackPanel Orientation="Vertical">
<Label Height="28" Width="70" Name="label1">Label</Label>
<Button Height="23" Width="70" Name="button1" Click="button_Click">Button</Button>
</StackPanel>
</Grid>
</Window>
Note that in the Visual Basic XAML you have Window x:Class="Window1" at the top (in case you’re wondering why the XAML does not compile). In the code behind, you have the following code that instantiates a DispatcherTimer in the load event of the Window, and allows a user from starting and stopping the timer by toggling the button click event.
Visual Basic
Class Window1
Dim timer As System.Windows.Threading.DispatcherTimer
Public Event UpdateTime()
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
timer = New System.Windows.Threading.DispatcherTimer()
timer.Interval = TimeSpan.FromSeconds(1)
AddHandler timer.Tick, AddressOf timer_Tick
End Sub
Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Me.label1.Content = DateTime.Now.ToLongTimeString()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
If Not Me.timer.IsEnabled Then
Me.timer.Start()
Me.button1.Content = "Stop"
Else
Me.timer.[Stop]()
Me.button1.Content = "Start"
End If
End Sub
End Class
C#
using System;
using System.Windows;
namespace WpfTimer
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
System.Windows.Threading.DispatcherTimer timer;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
this.label1.Content = DateTime.Now.ToLongTimeString();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (!this.timer.IsEnabled)
{
this.timer.Start();
this.button1.Content = "Stop";
}
else
{
this.timer.Stop();
this.button1.Content = "Start";
}
}
}
}