Maximum Timer interval

后端 未结 9 1273
心在旅途
心在旅途 2021-01-04 01:58

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.

相关标签:
9条回答
  • 2021-01-04 02:25

    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.

    0 讨论(0)
  • 2021-01-04 02:28

    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();
    }
    
    0 讨论(0)
  • 2021-01-04 02:28
    int days = 30;
    

    Create a timer for a period one day.

    Decrement days each time the timer fires.

    0 讨论(0)
提交回复
热议问题