17

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 days. How can I resolve it? Please help.

xyz
  • 27,223
  • 29
  • 105
  • 125
hovkar
  • 1,381
  • 3
  • 14
  • 22
  • 2
    which timer?? There are at least 3 different timers in .NET ! – marc_s Oct 26 '09 at 13:29
  • 7
    It's not a bug by the way. A bug is when your code and your specifications disagree. I think you'll find that this is a well documented limitation. – paxdiablo Oct 26 '09 at 13:33
  • 8
    You can reliably expect your process to run for 30 days? Sounds like a flaky approach. Such long-running processes should be process-independent... state should persist from the end of one process to the beginning of the next. – Rex M Oct 26 '09 at 13:35
  • Thats not a bug, its a constraint (because of the type int)! – Frank Bollack Oct 26 '09 at 13:37
  • 1
    @Rex Maybe it schedules a special celebration when the machine stays up for a month.. ;) –  Oct 26 '09 at 13:57
  • And, on top of that, has anyone out there actually *seen* a Windows box run for anywhere *near* 30 days without crashing? Pax ducks for cover :-) – paxdiablo Oct 26 '09 at 13:58
  • @paxdiablo: I assume you were joking, but we have lots of *servers* that run for months at a time. – MusiGenesis Oct 26 '09 at 14:02
  • @paxdiablo: I usually have my machine run non-stop between LAN parties. (Which are the second Saturday of each month.) I rarely reboot. – John Gietzen Oct 26 '09 at 14:40
  • 2
    @Rex, I've made a wrapper for System.Threading.Timer that does not have the limitation of 2*32 milliseconds (approx. 49 days). One place where I use it is during the startup of a server program to start a timer that will fire when the customer's license expires. This may be in two days or two years. Now, it doesn't matter if the server actually runs for two years or not - when the server is restarted my program is restarted and restarts the timer with a new time-to-expire calculation. But it is a simple and useful application for a timer that can run for a very long period. – RenniePet Jun 13 '11 at 00:17

9 Answers9

25

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();
}
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • 1
    And now you only have to keep your system healthy and happy for 30 days. – H H Oct 26 '09 at 13:58
  • 3
    @Henk: the question said nothing about *why* the timer's period needs to be 30 days. For all we know, this timer may be used to send an email to somebody that says "hey, this server has been running for 30 straight days". – MusiGenesis Oct 26 '09 at 14:00
  • 9
    30 days, yes. But the max is about 49 days because even if you use the int64 or TimeSpan versions it does not accept a value of more than 2*32-2 milliseconds. – RenniePet Jun 13 '11 at 00:11
  • @RenniePet: do you mean the constructor for `Timer` will throw an exception if the TimeSpan value is more than 49 days? Because a TimeSpan can hold a much longer interval than 49 days. – MusiGenesis Apr 23 '13 at 13:29
  • 9
    That's right. You get an "ArgumentOutOfRangeException", with the text "Time-out interval must be less than 2^32-2." – RenniePet Apr 23 '13 at 14:59
14
int days = 30;

Create a timer for a period one day.

Decrement days each time the timer fires.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
  • 2
    I'd agree. Timers aren't really intended to cope with time periods as extended as 30 days, although I'd have my timer run more than once per day. – ChrisBD Oct 26 '09 at 13:33
  • 6
    System.Threading.Timer is limited to about 49 days (2*32 milliseconds), even using the int64 and TimeSpan versions. So for a general-purpose timer for very long periods something like this is a valid approach. – RenniePet Jun 13 '11 at 00:51
10

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.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
4

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

4

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 1
    This is not an answer to the question. It is something completely different if you start an application at a given time (scheduler) or if your running application is doing something at a given time (timer). – Stefan Steinegger Mar 28 '12 at 10:12
3

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

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
1

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.

H H
  • 263,252
  • 30
  • 330
  • 514
1

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

Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
-1

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.