开发者

Maximum Timer interval

The maximum interval of timer is 2,147,483,647. it's about 25 days. But in my requirements I need that the timer will wait 30 day开发者_如何学JAVAs. How can I resolve it? Please help.


Use a System.Threading.Timer for this. There are constructors that take a long, a uint or a TimeSpan instead of an int for the dueTime. Any of these will let you set a period of 30 days.

Update: this is the easiest way to do it:

System.Threading.Timer _timer;
public void Start30DayTimer()
{
    TimeSpan span = new TimeSpan(30, 0, 0, 0);
    TimeSpan disablePeriodic = new TimeSpan(0, 0, 0, 0, -1);
    _timer = new System.Threading.Timer(timer_TimerCallback, null, 
        span, disablePeriodic);
}

public void timer_TimerCallback(object state)
{
    // do whatever needs to be done after 30 days
    _timer.Dispose();
}


int days = 30;

Create a timer for a period one day.

Decrement days each time the timer fires.


You can always have the timer wait a shorter amount of time, like one day and each time the timer fires increment a counter and check the current value of the counter. If the value is 30 then execute the code that is supposed to run once every 30 days.

A better idea is to set the value some where in an external file so that if the program ever unexpectedly quits, you can resume where you left off. Ideally you'd probably want to put the next run date in the file, so that if the program is off for multiple days you can resume and have it fire on the correct date without having to worry about when the last time it ran. So something like

NextRunDate = 10/10/2009 4:40 PM

When the program runs it sets the date ahead another 30 days.


This is the timer telling you your application is more properly implemented as a scheduled task.


I highly suggested using the Windows Scheduled Task feature, especially if this is intended to run something on the same day each month, as months vary between 28 and 31 days.


Set a datetime variable to the datetime when you want to execute whatever it is... Then just have the timer check every minute, (or whatever your level of required accuracy is) to see if that datetime has past...


To do that with a timer you also need an application and a PC that runs without interruption for 30+ days. Not impossible but a big risk.

It sounds like you want scheduled execution of something. Calculate the target date/time and persist that it an XML or .config file. When the application restarts it can re-calculate the TimeSpan for firing the event.


The Quartz.NET library is another option for running your code on a scheduled basis.


The timer constructor will take a TimeSpan which is int64 ticks surely that's over 25 days?

Have you also considered that the system may not stay up for 30 days?

Just looked it up, it's 10,675,199 days.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜