Time of day based timer - run function at certain time, daily I need to call a function exactly at certain hour, daily, my programs runs 24/7.

I wrote the following timer mechanism, which generally works that way:


Calculate time until hour and generate timer with that interval. 
When timer elapse, call timer event and generate another 24h timer. 
Go back to step 2.




private static void InitTimeBasedTimer(TimeSpan timeToRun, Action funcToRun)
{
    TimeSpan interval = timeToRun.Subtract(DateTime.Now.TimeOfDay);
    double intervalMillis = interval.TotalMilliseconds;
    if (interval.TotalMilliseconds &lt; 0)
    {
        intervalMillis = (new TimeSpan(24, 0, 0)).Add(interval).TotalMilliseconds;
    }
    Timer timer = new Timer(intervalMillis);
    timer.AutoReset = false;
    timer.Elapsed += (sender, e) =&gt; TimeBasedCallback(timeToRun, funcToRun);
    timer.Start();
}

public static void TimeBasedCallback(TimeSpan timeToRun, Action funcToRun)
{
    funcToRun();
    TimeSpan interval = timeToRun.Subtract(DateTime.Now.TimeOfDay); //24 h
    double intervalMillis = interval.TotalMilliseconds;
    if (intervalMillis &lt; 0)
    {
        intervalMillis = (new TimeSpan(24, 0, 0)).Add(interval).TotalMilliseconds;
    }
    Timer timer = new Timer(intervalMillis);
    timer.AutoReset = false;
    timer.Elapsed += (sender, e) =&gt; TimeBasedCallback(timeToRun, funcToRun);
    timer.Start();
}


EDIT:
There are several functions that each writes to a DB every day in different hours. Can't afford task scheduler / process for each function. Quartz.NET seems a bit overkill for such a simple task.
Without knowing what you're trying to do, I'd say this is the wrong approach. I would create an executable and set it up as a scheduled task to run every 24 hours. 

Not sure what OS you are using, but assuming it's windows its fairly easy http://windows.microsoft.com/en-au/windows7/schedule-a-task