Creating a timer


Lets move aways from the RSS readers… I hear some guys heaving a sigh of relief… hehe!! So lets start off with a timer which doesnt work as we usually expect it to..it works backwards!! Isnt that what we all wish. Anyway coming to the point, lets have a timer which counts down and on reaching ZERO plays a song. Sounds like a good app to wake you up when you plan to go for a short nap.


The design is simple. We have a grid with the hour/min/secs and buttons for Starting and Reseting. There is a TextBox at the bottom which gives some status messages. 🙂


The code looks like this in XAML:


Window1.xaml

<Window x:Class=”timer.Window1″

    xmlns=http://schemas.microsoft.com/winfx/avalon/2005

    xmlns:x=http://schemas.microsoft.com/winfx/xaml/2005

    Title=”timer” BorderThickness=”1″ Height=”90″ Width =”280″

    >


      <StackPanel>

            <Grid  >

                  <Grid.ColumnDefinitions >

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                        <ColumnDefinition Width=”auto”/>

                  </Grid.ColumnDefinitions>


                  <Grid.RowDefinitions>

                        <RowDefinition/>

                        <RowDefinition/>

                  </Grid.RowDefinitions>


                  <TextBox Grid.Column=”0″ Grid.Row=”0″ Text=”0″ Name=”hr”
Width=”30″ BorderThickness=”0″/>

                  <Label Grid.Column=”1″ Grid.Row=”0″ Content=”:”/>

                  <TextBox Grid.Column=”2″ Grid.Row=”0″ Text=”0″ Name=”min”
Width=”30″ BorderThickness=”0″/>

                  <Label Grid.Column=”3″ Grid.Row=”0″ Content=”:”/>

                  <TextBox Grid.Column=”4″ Grid.Row=”0″ Text=”0″ Name=”sec”
Width=”30″ BorderThickness=”0″/>

                  <Button Grid.Column=”5″ Grid.Row=”0″ Content=”Start” Click=”OnClick” />

                  <Button Grid.Column=”6″ Grid.Row=”0″ Content=”Reset” Click=”OnClick1″ />

                  <TextBox Grid.ColumnSpan=”7″ Grid.Row=”1″ Text=”Enter Timer Value”
IsEnabled=”False” Name=”status”  BorderThickness=”1″/>


            </Grid>

      </StackPanel>

</Window>


Window1.xaml.cs

    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;


        private void OnClick(object sender, RoutedEventArgs e)
        {
            if (ValidateValues(hr.Text, min.Text, sec.Text))
            {
                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();
            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

        }


    }


    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

        }


        public string TimeLeft()
        {
            return (hr.ToString() + “:” + min.ToString() + “:” + sec.ToString());
        }
    }


 


The new things in play that we see here are the SoundPlayer and the Timer which fit in seamlessly with the Avalon code.


So cut short your naps with a simple timer!!