强制计时器在设定的时间内循环 [C#]

发布于 2024-09-08 01:02:46 字数 705 浏览 0 评论 0原文

我正在用 C# .Net 重写 VB 6.0 程序以进行实习。它测量来自精密磨床的输入数据并给出其精度的读数。程序的大部分内容都是在 Timer 对象内完成的,间隔为 10。该程序尚未完成,因此尚未在实际机器上进行测试,但我有一个生成正弦波的调试模拟(以确保在收到数据后,拨号盘和数字均按预期运行)。

我正在使用 WinForms 和 Timer 对象,而不是 System.Threading.Timer (尽管如果这听起来是一个更好的选择,无论如何我都愿意使用它)。我使用 System.Diagnostics.Stopwatch ElapsedTicks 来计算计时器从开始到结束循环平均需要多少个滴答声。在 Form1 上,计时器通常需要大约 19500 个时钟周期。当它切换到 Form2 时,这个数字会下降到 15000 左右,在 Form3 上甚至更低到 11000。

现在,Form3 的计算速度更快是有道理的,因为它跳过了一些不需要运行的代码部分。 Form2 应该运行整个计时器。奇怪的是,切换到Form2,然后再回到Form1后,计时器保持在16000左右,并且每次切换时都会稍微快一些。间隔始终保持恒定 10。

我不确定它在做什么,或者如何强制它以恒定速率运行。此外,我不确定这是否重要。它一直困扰着我,但我不确定当程序从机器接收实际数据时它的评估速度是否会重要。

如果有人对计时器为何如此运行,或者如何限制其以恒定速率运行,或者即使我是否应该担心有任何建议,他们将不胜感激。

感谢您抽出时间

I'm rewriting a VB 6.0 program in C# .Net for an internship. It measures incoming data from a precision grinding machine and gives a readout on its accuracy. A lot of the meat of the program is done within a Timer object, with an interval of 10. The program is unfinished, so it has yet to be tested on an actual machine, but I have a Debug simulation that generates a Sin wave (to ensure that, upon receiving data, the dials and numbers all operate as expected).

I'm using WinForms, and the Timer object, not System.Threading.Timer (though if that sounds like a better option, by all means I'm open to using it). I used System.Diagnostics.Stopwatch ElapsedTicks to time just how many ticks it was taking the Timer to cycle from beginning to end, on average. On Form1, the timer generally takes around 19500 ticks. When it switches to Form2, that drops to around 15000, and even lower to 11000 on Form3.

Now, it makes sense that Form3 is evaluating faster, because it skips some parts of the code that don't need to be run. Form2 should be running the entire Timer. What's stranger is that after switching to Form2, and then back to Form1, the timer remains at around 16000, and gets slightly faster every time I switch between forms. The interval always stays a constant 10.

I'm not sure what's doing it, or how I can force it to run at a constant rate. Furthermore, I'm not sure if it even matters. It's been bugging me immensely, but I'm not sure it will matter that it's evaluating faster when it program is receiving actual data from a machine.

If anyone has any suggestions as to why the timer is acting the way it is, or how to limit it to run at a constant rate, or to even if I should worry about it or not, they would be greatly appreciated.

Thanks for your time

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

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

发布评论

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

评论(1

身边 2024-09-15 01:02:46

当您指的是周期时,您是指执行事件逻辑所需的时间还是计时器滴答之间的时间? UI 计时器可能不那么准确,因为它在 UI 线程上运行。

老实说,听起来您需要实时要求,但您不会轻易(或根本)获得这些要求。您还必须应对自始至终任意运行的垃圾收集。

我不会担心这个问题,尽管我可能会使用线程计时器,而不是 UI 计时器。

When you mean cycle, do you mean the time it takes to execute the event logic or the time between timer ticks? The UI timer may not be that accurate because it operates on the UI thread.

To be honest, it sounds like you need real-time requirements, something that you aren't going to get easily (or, at all). You also have to contend with garbage collection arbitrarily running throughout.

I wouldn't worry about it, though I would possibly use a threaded timer, not a UI timer.

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