我想用C#做的东西在分钟每分钟(由时钟)的Windows窗体应用程序。我只是想知道最新最好的路要走呢?
I want to do stuff every minute on the minute (by the clock) in a windows forms app using c#. I'm just wondering whats the best way to go about it ?
我可以用一个定时器和间隔设置为60000,但要获得它的运行分钟,我将不得不启用它的微小precisely,没有真正可行的。
I could use a timer and set its interval to 60000, but to get it to run on the minute, I would have to enable it on the minute precisely, not really viable.
我可以用一个定时器和间隔时间设置为1000。那么它的Tick事件中,我可以检查时钟当前分钟对我设置一个变量,如果微小改变,然后运行我的code。这令我很担心,因为我让我的电脑做一次检查,以便开展工作,每隔1分钟,每1秒。当然,这是丑陋的?
I could use a timer and set its interval to 1000. Then within its tick event, I could check the clocks current minute against a variable that I set, if the minute has changed then run my code. This worries me because I am making my computer do a check every 1 second in order to carry out work every 1 minutes. Surely this is ugly ?
我使用的是Windows窗体和NET 2.0所以不希望使用自带的.Net 3.5 DispatchTimer
I'm using windows forms and .Net 2.0 so do not want to use the DispatchTimer that comes with .Net 3.5
这必须是一个相当普遍的问题。有任你一个更好的方式来做到这一点?
This must be a fairly common problem. Have any of you a better way to do this?
推荐答案大厦答案从阿奎那可以漂移,这不正是对刚分钟内分一秒滴答:
Building on the answer from aquinas which can drift and which doesn't tick exactly on the minute just within one second of the minute:
static System.Timers.Timer t; static void Main(string[] args) { t = new System.Timers.Timer(); t.AutoReset = false; t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); t.Interval = GetInterval(); t.Start(); Console.ReadLine(); } static double GetInterval() { DateTime now = DateTime.Now; return ((60 - now.Second) * 1000 - now.Millisecond); } static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Console.WriteLine(DateTime.Now.ToString("o")); t.Interval = GetInterval(); t.Start(); }在我的盒子此code一贯蜱每分钟.02s内:
On my box this code ticks consistently within .02s of each minute:
2010-01-15T16:42:00.0040001-05:00 2010-01-15T16:43:00.0014318-05:00 2010-01-15T16:44:00.0128643-05:00 2010-01-15T16:45:00.0132961-05:00更多推荐
.NET,每分钟(在分钟)事件。是一个定时器的最佳选择?
发布评论