문제

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...

도움이 되었습니까?

해결책

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));
    }
}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top