xaml code
--------------------------------------------------------------------------------------
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
-------------------------------------------------------------------------------
cs code
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace TimerExample
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
private Clock clk; //This is a class which keeps track of the time being displayed
private System.Windows.Threading.DispatcherTimer TimerClock;
int changepic = 0;
public Window1()
{
InitializeComponent();
}
private void OnClick(object sender, RoutedEventArgs e)
{
//if (ValidateValues(hr.Text, min.Text, sec.Text))
//{
status.Text = "starting...";
TimerClock = new System.Windows.Threading.DispatcherTimer();
//Creates a timerClock and enables it
TimerClock.Interval = new TimeSpan(0, 0, 1);
TimerClock.IsEnabled = true;
clk = new Clock(hr.Text, min.Text, sec.Text);
TimerClock.Tick += new EventHandler(TimerClock_Tick);
//}
}
private void OnClick1(object sender, RoutedEventArgs e)
{
TimerClock.IsEnabled = false;
Clock.hr = Clock.min = Clock.sec = 0;
hr.Text = Clock.hr.ToString();
min.Text = Clock.min.ToString();
sec.Text = Clock.sec.ToString();
}
void TimerClock_Tick(object sender, EventArgs e)
{
clk.Decrement();
hr.Text = Clock.hr.ToString();
min.Text = Clock.min.ToString();
sec.Text = Clock.sec.ToString();
//change pic
changepic = Clock.sec % 6+1;
string text = "cat0" + changepic.ToString()+".gif";
//myImage.Source = "cat02.gif";
myImage.Source = new BitmapImage(new Uri(text, UriKind.Relative));
if (Clock.completed)
{
TimerClock.IsEnabled = false;
//SoundPlayer player = new SoundPlayer(@"gurgle.wav");
//Plays the spoilsport of your sleep ;)
//player.Play();
}
}
private bool ValidateValues(string hr1, string min1, string sec1)
{
//Validate values
return true;
}
}
public partial class Clock
{
public static int hr, min, sec;
public static bool completed;
public Clock() { }
public Clock(string hr1, string min1, string sec1)
{
completed = false;
hr = int.Parse(hr1);
min = int.Parse(min1);
sec = int.Parse(sec1);
}
public void Decrement()
{
//Decrement the hours/mins/secs
sec++;
if (sec >= 60)
min =(int)Math.Floor((decimal)sec / 60);
if (sec >= 3600)
hr = (int)Math.Floor((decimal)sec / 3600);
}
public string TimeLeft()
{
return (hr.ToString() + ":" + min.ToString() + ":" + sec.ToString());
}
}
}
---------------------------------------------------------------------------------------