C# 最大定时器间隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1624789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum Timer interval
提问by Wilson
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.
定时器的最大间隔为 2,147,483,647。这是大约25天。但在我的要求中,我需要计时器等待 30 天。我该如何解决?请帮忙。
采纳答案by MusiGenesis
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.
System.Threading.Timer
为此使用 a 。有迹象表明,需要构造函数long
,一个uint
或一个TimeSpan
代替的int
了dueTime
。其中任何一个都可以让您设置 30 天的期限。
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();
}
回答by Bob Kaufman
int days = 30;
Create a timer for a period one day.
为某一天创建一个计时器。
Decrement days
each time the timer fires.
days
每次定时器触发时递减。
回答by kemiller2002
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.
你总是可以让计时器等待更短的时间,比如一天,每次计时器触发时都会增加一个计数器并检查计数器的当前值。如果值为 30,则执行应该每 30 天运行一次的代码。
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
NextRunDate = 2009 年 10 月 10 日下午 4:40
When the program runs it sets the date ahead another 30 days.
当程序运行时,它会将日期提前 30 天。
回答by Charles Bretana
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...
将日期时间变量设置为您想要执行任何内容的日期时间......然后让计时器每分钟检查一次,(或任何您所需的准确性级别),看看该日期时间是否已经过去......
回答by Henk Holterman
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.
要使用计时器做到这一点,您还需要一个应用程序和一台可以不间断运行 30 多天的 PC。并非不可能,但风险很大。
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.
听起来您想要按计划执行某事。计算目标日期/时间并将其保存为 XML 或 .config 文件。当应用程序重新启动时,它可以重新计算触发事件的时间跨度。
回答by Henk Holterman
This is the timer telling you your application is more properly implemented as a scheduled task.
这是一个计时器,告诉您您的应用程序作为计划任务更合适地实现。
回答by Henk Holterman
The timer constructor will take a TimeSpan which is int64 ticks surely that's over 25 days?
计时器构造函数将采用一个 TimeSpan,它肯定是超过 25 天的 int64 滴答声?
Have you also considered that the system may not stay up for 30 days?
您是否也考虑过系统可能无法保持 30 天?
Just looked it up, it's 10,675,199 days.
刚刚查了一下,是10675199天。
回答by Powerlord
I highly suggested using the Windows Scheduled Taskfeature, especially if this is intended to run something on the same day each month, as months vary between 28 and 31 days.
我强烈建议使用 Windows计划任务功能,特别是如果这是为了在每个月的同一天运行某些东西,因为月份在 28 到 31 天之间变化。
回答by Joel Mueller
The Quartz.NETlibrary is another option for running your code on a scheduled basis.
该Quartz.NET库是一个计划的基础上运行的代码的另一种选择。