一个应用程序中包含多个 DispatcherTimer
我有一个行为类似于自动刷新的应用程序。它会检查电子邮件、新视频、笑话等。
我的客户希望以不同的时间间隔检查这些单个元素。例如,每分钟发送电子邮件,每小时发送视频等。因此,他应该可以选择将时间间隔自己写到文本框中,并通过选中适当的复选框开始/停止刷新。
应用程序是用 wpf 编写的。我的问题是,添加更多 DispatcherTimers
(DT) 是否是更好的选择,每个元素一个,还是只使用一个 DT
并在勾选功能进行切换?
另外,我假设 DT 的 tick 方法运行主线程。我说得对吗?如果可能的话,在不同线程上运行tick方法内的操作是否更好?
I have an application behaving like autorefresh. It checks emails, new videos, jokes, etc.
My client wants these single elements check in different intervals. For example emails every minute, videos every hour, etc. So there should be option for him to write down intervals by himself into textboxes and by checking appropriate checkbox start/stop refreshing.
Application is written in wpf. My question is, whether is better option to add more DispatcherTimers
(DT), one for each of elements or stick with only one DT
and in tick function make switch?
Also, I assume that DT
's tick method runs main thread. Am I right? Is it better to run operations inside tick method on different thread if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让它尽可能简单。只要检查新元素并不耗时,只需为每个元素类别使用一个带有自己的
Tick
处理程序的DispatcherTimer
(当然,如果您不这样做)有数百个类别)。如果检查操作很耗时,它将阻塞
Tick
处理程序,从而阻塞 UI 线程。然后,您可以每使用一个 System.Timers.Timer元素类别并在计时器的Elapsed
处理程序(而不是Tick
)中执行新元素检查。由于计时器在不同的线程(来自线程池)上运行,因此您必须与 UI 线程同步:Make it as simple as possible. As long as it is not time-consuming to check for new elements, simply use one
DispatcherTimer
with its ownTick
handler per element category (and of course if you don't have hundreds of categories).If the check operation is time-consuming it would block the
Tick
handler and thus the UI thread. Then you could use one System.Timers.Timer per element category and perform the check for new elements in the Timer'sElapsed
handler (instead ofTick
). Since the timers run on different threads (from the thread pool) you would have to synchronize with the UI thread:默认情况下,WPF 应用程序具有单线程 - 主 UI 线程,所有 UI 控件/窗口均被创建并与其关联。在这种情况下,我没有看到使用多个调度程序和调度程序计时器有任何好处,因为无论如何调度程序计时器会将所有消息委托给关联的调度程序的消息循环(主 UI 线程消息循环)。
但是,如果您在单独的工作线程中创建了一些控件,例如
并且也会将消息发布到此窗口 - 那么创建一个新的调度程序并与手动创建的线程关联是有意义的,因此基本上每个调度程序都将与自己的线程关联,所以关系是 1 比 1。
关于 atick 方法 - 它将在与 Dispatcher 线程关联的线程上执行。如果您尚未创建与手动创建的工作线程关联的调度程序,那么默认情况下 WPF 调度程序与主 UI 线程关联,
MSDN:
By default WPF Application has single thread - Main UI thread and all UI controls/windows are created and associated with it. In this case I do not see any benefits usign multiple Dispatchers and Dispatcher Timers since anyway Dispatcher Timer would delegate all messages to the associated Dispatcher's messages loop what would be a Main UI thread message loop.
But if you've created some controls in separate worker threads like
And would post messages to this window as well - then it makes sense creating a new Dispatcher and associating with a manually created thread, so basically each Dispatcher will be associated with own thread, so relation is 1 to 1.
Regarding atick method - it would be executed on the associated with Dispatcher thread. If you've not created dispatcher associated with a manually created worker thread, so by default WPF Dispatcher associated with the main UI thread,
MSDN: