Exercise: Clock event

In class I showed and example CountDownTimer. In this example you start a timer, that counts down to zero. At each "tick" it sends an event.

In this exercise you are going to make something similar. You must program a class Clock that fires an event with certain intervals (like every second). The Clock never stops (unlike the CountDownTimer).

Getting ready

  1. Download, read and understand the example CountDownTimerEventArgs
  2. Create a new Solution in Visual Studio

EventArgs class

First you are going to make a class similar to TickEventArgs from the CountDownTimer example.

Make a class TimeEventArgs. This class must have a property

  public DateTime TimeNow { get; private set; }

The value of the property must be initialized in the constructor, use DateTime.Now to get the current time.

Clock class

Next you are going to make a class similar to CountDownTimer.

Make a class Clock. Some fragments that might be useful

Main

In th main make an object of the Clock class, and add one or more event handlers to the Clock.Tick event.

The event handler(s) can do Console.WriteLine(...)

Extra: Slightly more complicated event handler

Add another event handler: This handler is only going to do Console.WriteLine(...) every 10 seconds.

Variable sleep time

In the current version I assume the time between Tick is 1000 msec (1 second).

Adapt the class Clock so that the time can very according to a parameter to the Clock constructor.

Extra: Default value

For backward compatibility you can assign this parameter a default value.

Help (section "Optional Arguments)

Extra: Make a GUI

Problem: I assume you made Visual Studio Solution a Console Application.This means that you cannot make GUI applications.

Solution: Add another project to the EXISTING Solution: Right click the Solution and "New Project ...", choose WPF