开发者

C# Aperiodic Event Generation

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 nex开发者_运维问答t saved time that is in my database... how to do it...


You should take a look at Quartz.NET.

From the Features of Quartz.Net:

Quartz.NET can run embedded within another free standing application


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


Timer Class

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜