system.threading.timer无法正常工作 [英] system.threading.timer can not worked

查看:94
本文介绍了system.threading.timer无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将计时器设置为在1分钟后执行时,我在Windows服务中使用了system.threading.timer.从12:00:00开始.

i have used the system.threading.timer in windows service when i have set the timer to execute after 1 min. starting from 12:00:00.

timer1[timer] = new System.Threading.Timer(timerDelegate, obj, dueTime, interval);



在此间隔执行的计时器

12:00:00
12:00:59
12:01:00
12:02:00
12:02:59

但我想在
上课 12:00:00
12:01:00
12:02:00
12:03:00


为什么计时器从12:00:59开始?

我已经按照代码
设置了要执行的余数



timer excuted at this interval

12:00:00
12:00:59
12:01:00
12:02:00
12:02:59

but i want to excute at
12:00:00
12:01:00
12:02:00
12:03:00


why timer started at 12:00:59 ?

i have set the remainder for excution following code

declaration
            _interval[timer] = interval;
            _startTime[timer] = DATeTime + dueTime;

used in timer callback method

long elapsedMs = Convert.ToInt64((dt - _startTime[timer]).TotalMilliseconds);
                   long intervalMs = Convert.ToInt64(_interval[timer].TotalMilliseconds);
                   long remainder = elapsedMs % intervalMs;
                   if (remainder != 0L)
                   {
                       timer1[timer].Change(_interval[timer] - TimeSpan.FromMilliseconds(remainder), _interval[timer]);
                   }




当在Windows XP中使用相同的应用程序时,它可以完美唤醒,但在Windows 7及更高版本中Windows Server 2008无法正常工作.
如何解决threading.timer中的此问题?

感谢




when used the same application in windows XP it''s woking perfectly but in windows 7 & windows server 2008 its not working.
how to resolved this problem in threading.timer ?

Thanks

推荐答案

Windows不是时间紧迫的操作系统,因此您无法保证时序...任何其他应用程序都可能引起轻微的延迟,从而导致时间要晚一点.这完全取决于系统何时决定执行其任务切换.

另外,您将时间截断为毫秒,因此您可以将计时器设置为刚好不足以达到下一分钟的时间.这可能与XP和较新的操作系统有所不同.也许那台计算机上的时间分辨率是1毫秒.分辨率可能取决于硬件,并且多年来一直在增加.也许需要额外的毫秒数.

另一种可能性是在精确分钟后几毫秒内计算下一次时间.时间缩短到几秒钟后,您将看不到差异(除非您的计算机冻结了大约一秒钟).
Windows is not a time-critical OS thus you won''t have guaranteed timing... Any other application can cause a slight delay and thus cause the time to be a bit late. It all depend on exactly when the system decide to do its task switching.

Also, you truncate time to milliseconds, so you might set the timer with a time that would be just not enough to reach the next minute. This might be a difference bewteen XP and more recent OS. Maybe the resolution of the time on that computer was 1 milliseconds. The resolution probably depends on the hardware and has increased over years. Maybe adding an extra millisecond would be what you need.

Another possibility would be to compute next time a few milliseconds after the exact minute. After the time is truncated to seconds, you won''t see the difference (except if your computer freeze for about a second).


这篇关于system.threading.timer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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