threading.timer根据系统时钟变化执行 [英] threading.timer excute according to the system clock changes
问题描述
我已经使用system.threading.timer在1小时后执行特定任务.我已经将以下代码用于计时器
I have used the the system.threading.timer to excute the particular task after 1 hour .i have used the following code for the timer
TimeSpan tsDueTime = new TimeSpan(1, 0, 0);
TimeSpan tsPeriod = new TimeSpan(1, 0, 0);
TimerCallbacktime rDelegate = new TimerCallback(databaseTrensfer);
timer1[timer] = new System.Threading.Timer(timerDelegate, obj, dueTime,interval);
它会在1小时后正确执行计时器.但是当我在1:30启动应用程序时,它将在2:30--3:30--4:30在1小时后执行计时器,但是我想执行根据系统时钟的时间,例如,当我在1:30启动计时器后,下一个计时器在2:00 --3:00--4:00执行,如何设置threading.timer根据系统执行
it excute the timer after 1 hour correctly .but when i have started the application at 1:30 then it will excute the timer after 1 hour at 2:30--3:30--4:30.but i want to excute the time according to the system clock like , when i have started the timer at 1:30 then next timer excute at 2:00 --3:00--4:00 .how to set the threading.timer to excute according to the system clock changes?
推荐答案
您可以结合使用System.DateTime.Now
, ^ ].
假设您获得了当前时间,并确定需要下一个事件(以毫秒为单位),该时间对应于23:13:329(分钟:秒:毫秒).为该时间段设置一个计时器,以下一个以小时为单位的边界到达下一个事件,然后将计时器更改为每小时计时一次.这样的事情.
—SA
You can do it in combination with polling of the current time usingSystem.DateTime.Now
, http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
Suppose you get a current time and determine that you need next event in a number of milliseconds which corresponds to 23:13:329 (minutes:seconds:milliseconds). Set a timer for this period to come to the next event at the boundary rounded by hours, and then change the timer to tick hourly. Something like that.
—SA
这篇关于threading.timer根据系统时钟变化执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!