So i want to check the current time
every second and if seconds == 0
raise event
and show current time:
using System.Timers;
public delegate void TimeHandler(object sender, TimeEventArgs e);
public class Clock
{
private Timer timer;
public event TimeHandler CurrentTime;
public Clock()
{
timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
}
public void Start()
{
timer.Enabled = true;
timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime time = DateTime.Now;
int sec = time.Second;
if (sec == 0)
if (CurrentTime != null)
CurrentTime(this, new TimeEventArgs(time));
}
}
public class TimeEventArgs
{
public readonly DateTime Time;
public TimeEventArgs(DateTime time)
{
Time = time;
}
}
Usage:
Clock clock = new Clock();
clock.CurrentTime += Clock_CurrentTime;
clock.Start();
private static void Clock_CurrentTime(object sender, TimeEventArgs e)
{
Console.WriteLine(e.Time.ToShortTimeString());
}
But it seems like the timer elapsed not starting.