threading.timer根据系统时钟变化执行 [英] threading.timer excute according to the system clock changes

查看:82
本文介绍了threading.timer根据系统时钟变化执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用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
You can do it in combination with polling of the current time using System.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆