hovkar
hovkar

Reputation: 1421

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

Upvotes: 17

Views: 26970

Answers (9)

Joel Mueller
Joel Mueller

Reputation: 28764

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

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273514

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.

Upvotes: 1

user159335
user159335

Reputation:

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.

Upvotes: -1

Powerlord
Powerlord

Reputation: 88816

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.

Upvotes: 4

MusiGenesis
MusiGenesis

Reputation: 75346

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

Upvotes: 25

user1228
user1228

Reputation:

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

Upvotes: 4

Charles Bretana
Charles Bretana

Reputation: 146559

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

Upvotes: 3

kemiller2002
kemiller2002

Reputation: 115528

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.

Upvotes: 11

Bob Kaufman
Bob Kaufman

Reputation: 12835

int days = 30;

Create a timer for a period one day.

Decrement days each time the timer fires.

Upvotes: 15

Related Questions