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