.NET,每分钟(在分钟)事件。是一个定时器的最佳选择?

编程入门 行业动态 更新时间:2024-10-25 20:26:40
本文介绍了.NET,每分钟(在分钟)事件。是一个定时器的最佳选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用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,每分钟(在分钟)事件。是一个定时器的最佳选择?

本文发布于:2023-06-07 15:25:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   定时器   每分钟   最佳选择   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!