I have a question about a timer loop I am performing in my C# winforms app. Every 50 seconds it's supposed to check the time on the system, and compare it to a user defined time. If that time matches the current time, perform the function, if not then return to the timer and wait another 50 seconds. I thought I had it working correctly but after leaving the application running overnight I came back and my CPU was at 99% and it was using 3.5gb memory. I checked with the timer disabled and the rest of the application runs fine so it's got to be something with the timer.
here is the timer code:
public void day_timer()
{
System.Timers.Timer timer2 = new System.Timers.Timer();
timer2.Elapsed += new ElapsedEventHandler(daily_Elapsed);
timer2.Interval = 50000;
timer2.Enabled = true;
timer2.Start();
}
and here is the elapsed code:
private void daily_Elapsed(object sender, EventArgs e)
{
string time_rightmeow = DateTime.Now.ToString("hh:mm tt");
if (time_rightmeow != Configuration.Day_time)
{
day_timer();
}
else
{
day_backup();
}
}
as you can see it's pretty simple, but I am wondering if there is a more efficient way to do this. I am assuming that the timer is not being disposed properly or something to that effect.
any Ideas?