Implementing a timer thread
Timer threads are implemented in Silverlight applications using the DispatchTimer class, which is part of the System.Windows.Threading library. The DispatcherTimer implements threads that wake up on a specific interval and execute a defined function in the Silverlight application and then sleep. The UI thread in the Silverlight application is responsible for starting and stopping the DispatchTimer thread.
To implement a DispatchTimer thread, you need to create an instance of the class and then set the Interval property and Tick event handler of the DispatcherTimer object. The Interval property is a TimeSpan object that defines at what interval to wake up the thread.
FIGURE 13.4
[ Retrieve | My PersistentText
The Tick event handler is a RoutedEvent handler that points to a Silverlight function to run when the timer wakes up.
For example, the following code creates a DispatcherTimer thread named alarm that wakes up every 5 seconds and calls a function named WakeUp() :
DispatcherTimer alarm = new DispatcherTimer(); alarm.Interval = new TimeSpan(0, 0, 0, 5, 0); alarm.Tick += new EventHandler(WakeUp);
The DispatchTimer object also contains a Start() and Stop() method. The Start() method creates a timer thread and begins the interval wait. Each time the interval is reached, the Tick event handler is called. The thread continues to run until the Stop() method is executed.
The DispatchTimer thread actually runs on a UI thread and is able to directly ^ ^ access Silverlight controls without using BeginInvoke().
The code in Listings 13.11 and 13.12 shows Silverlight application examples that implement a DispatchTimer thread. The code in Listing 13.11 implements a TextBlock control to display the current time and a Button control that will be used to start the clock.
LISTING 13.11
XAML Code That Defines a TextBlock and Button Control That Creates a Clock in the Browser
<UserControl x:Class="TimerApp.Page"
xmlns="http://schemas.microsoft.com/client/2 007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="500" Height="180">
<Grid x:Name="LayoutRoot" Background="Black"> <TextBlock x:Name="myText" Foreground="Red" FontFamily="Arial Black" FontSize="80" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/> <Button x:Name="myButton" Content="Start Clock" VerticalAlignment="Bottom" Margin=M0,0,0,20M Height="20" Width=M100M/>
The code in Listing 13.12 imports the System.Windows.Threading library and defines a DispatchTimer class named timer. In the Page() constructor function the timer object is instantiated and the Interval is set to 1 second. The UpdateClock() Tick event handler is also attached to the timer object.
A Click event handler named StartClock() is attached to the Button control. Inside the StartClock() event handler the Start() and Stop() functions are used to begin and end the timer thread.
The results are shown in Figure 13.5. When the user clicks Start Clock, the timer thread launches and the Silverlight application begins to update the time. When Stop Clock is clicked, the timer thread stops and the clock stops updating.
LISTING 13.12
C# Code That Implements a DispatchTimer Thread to Start and Stop a Clock in the Browser using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace TimerApp {
public partial class Page : UserControl {
DispatcherTimer timer; bool isTicking;
InitializeComponent(); isTicking = false;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 1, 0); // 1 Second timer.Tick += new EventHandler(UpdateClock);
myButton.Click += new RoutedEventHandler(StartClock);
public void StartClock(object o, RoutedEventArgs sender) {
continued
LISTING 13.12
(continued)
myButton.Content = "Start Clock";
isTicking = false;
myButton.Content = "Stop Clock";
timer.Start();
isTicking = true;
public void UpdateClock(object o, EventArgs sender) {
this.myText.Text = String.Format("{0:T}", DateTime.Now)
FIGURE 13.5
Silverlight application that implements a basic clock
Average user rating: 5 stars out of 1 votes
Post a comment