Question

i have a list of events "Appointments" in my database, like at 3:00pm etc, and they have irregular intervals like one after 15 mins and next after 30 mins and next after 25 mins. One way is that i run a thread/timer that checks the database every minute whether it is the minute of next appointment or not.. i want some other mechanism by which i can trigger an alert/event/action on the next saved time that is in my database... how to do it...

Was it helpful?

Solution

Get the date/time of the next appointment in the database, subtract from that DateTime.Now in order to get a TimeSpan, and then use that timespan to fire the timer once, similar to this:

class MainClass
{
    public static void FireTimerAt(DateTime next)
    {
        TimeSpan waitTime = next - DateTime.Now;

        new Timer(delegate(object s) {
                        Console.WriteLine("{0} : {1}",
                        DateTime.Now.ToString("HH:mm:ss.ffff"), s);
                    }
              , null, waitTime, new TimeSpan(-1));
    }
}

OTHER TIPS

You should take a look at Quartz.NET.

From the Features of Quartz.Net:

Quartz.NET can run embedded within another free standing application

Timer Class

Provides a mechanism for executing a method at specified intervals. This class cannot be inherited.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top