.NET Timer 比 VB6 Timer 更可靠吗

发布于 11-30 03:05 字数 256 浏览 2 评论 0原文

在 VB6 的美好时光中,您不能依赖计时器控件来精确地按照指定的时间间隔触发。如果您的程序正在执行一些密集处理,则 Timer_Tick 事件会被推入堆栈,并且只有当它到达指令时才会被处理,这可能是几秒钟(?)之后。

所以我的问题是 - .NET 计时器控件是否已得到改进,以便可以可靠地按照指定的时间间隔触发?我想上面的内容仍然适用,不是吗?但它比 VB6 版本更好吗?

除了使用计时器控件之外,还有其他方法可以确保事件在指定的时间间隔后触发吗?

In the good old days of VB6 you could not rely on the timer control to fire at exactly the specified interval. If your program was doing some intense processing the Timer_Tick event is pushed onto the stack and only when it gets to the instruction is it processed, which may be some seconds(?) later.

So my question is - has the .NET timer control been improved such that this can be relied upon to fire at exactly the time interval specified? I guess the above still applies doesn't it? But is it any better than the VB6 version?

Are there any alternatives to using the timer control that ensure that an event fires after a specified interval?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果2024-12-07 03:05:50

System.Windows.Forms.Timer 在 UI 线程上运行并依赖于消息泵;它的工作原理与 VB6 定时器基本相同。

但.NET 还有其他计时器 - System.Threading.Timer、System.Timers.Timer 在多线程环境中使用工作线程。

哪个“更好”取决于您的要求 - 使用多线程计时器可能会更接近于请求的间隔触发,但这是以使用线程固有的复杂性增加为代价的。

System.Windows.Forms.Timer runs on the UI thread and relies on a message pump; it works essentially in the same way as a VB6 timer.

But .NET has other timers - System.Threading.Timer, System.Timers.Timer that use worker threads in a multi-threaded environment.

Which is "better" depends on your requirement - using a multithreaded timer may fire more nearly at the requested interval, but this comes at the cost of the added complexity inherent in using threading.

兮子2024-12-07 03:05:50

计时器的问题在于它在某种意义上依赖于操作系统。计时器工作必须由操作系统安排,如果有另一个高优先级进程占用了大量处理能力,那么它可能会决定安排您的代码稍后运行,从而为另一项更重要的工作提供更多时间进步。

在大多数情况下,较新的计时器可能更可靠,但它们的可靠性取决于子系统。我不确定 Windows 是否实现了硬件计时器(更可靠),或者 .NET VB 是否使用它(如果有的话),但从我过去的工作经验来看,它似乎是时好时坏。大多数时候,它几乎在很小的范围内准确无误,而其他时候,我发现它在某些条件下延迟了长达一秒。

我的建议是不要使用睡眠并使用计时器,这样您就不会有太多问题。

PS:它以精确的时间间隔发生相对困难,通常会有几毫秒的差异,因此您不应该依赖非常非常精确的计时器粒度。

The problem with Timers is that it's OS dependent in a sense. The timer work has to be scheduled by the OS and if there's another high priority process using up a lot of the processing power then it might decide to schedule your code to run at a later time giving more time for another more important line of work to progress.

For the most part newer timers are probably more reliable but they are only as reliable as the subsystem is. I'm not sure if windows implements hardware timers (which are more reliable) or if the .NET VB uses it if it does but from what I've worked with in the past it seems to be hit or miss. Most of the time it's almost dead on within a very small margin and other times I've seen it get delayed by up to a second under certain conditions.

My advice is don't use sleeps and use Timers instead and you shouldn't have very many problems.

P.S: It is relatively difficult for it to happen at EXACT time intervals, there will usually be a couple of milliseconds difference so you shouldn't rely on very very exact Timer granularity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文