计时器已用事件如何与高优先级线程竞争?
我有一个想要使用的 System.Timers.Timer 对象,但我不希望与 Timer 相关的处理干扰正常到高优先级的线程。换句话说,我想说的是,只要没有其他东西在运行,我希望每 5 秒处理一次 X。
如何确保我的计时器操作以低优先级方式运行?
I have a System.Timers.Timer object that I want to use, but I don't want the processing tied to the Timer to interfere with normal to high priority threads. In other words, I'd like to say that I want to process X every 5 seconds as long as nothing else is running.
How could I ensure that my Timer operations are running in a low-priority manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.Timers.Timer 的好处是,您可以通过
SynchronizingObject
属性分配一个同步对象,然后利用它来运行优先级可以为的线程的Elapsed
事件受控。只需将
ElapsedEventReceiver
的实例分配给计时器的SynchronizingObject
属性即可。免责声明:我很快就完成了这个,因此您需要添加自己的最后润色以使其更加健壮。
The nice thing about System.Timers.Timer is that you can assign a synchronzing object via the
SynchronizingObject
property and then exploit it to run theElapsed
event a thread whose priority can be controlled.Just assign an instance of the
ElapsedEventReceiver
to theSynchronizingObject
property of your timer.Disclaimer: I whipped this up pretty fast so you will need to add your own finishing touches to make it more robust.
也许最简单的方法是有一个“忙”标志(或计数),并忽略计时器滴答声,只要它不为零。
PS 不建议更改线程优先级。几乎没有必要。
Probably the easiest way would be to have a "busy" flag (or count), and ignore the timer ticks as long as it's non-zero.
P.S. Changing thread priorities is not recommended. It's hardly ever necessary.