一个应用程序中包含多个 DispatcherTimer

发布于 2025-01-06 13:12:38 字数 348 浏览 0 评论 0原文

我有一个行为类似于自动刷新的应用程序。它会检查电子邮件、新视频、笑话等。

我的客户希望以不同的时间间隔检查这些单个元素。例如,每分钟发送电子邮件,每小时发送视频等。因此,他应该可以选择将时间间隔自己写到文本框中,并通过选中适当的复选框开始/停止刷新。

应用程序是用 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 技术交流群。

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

发布评论

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

评论(2

本宫微胖 2025-01-13 13:12:38

让它尽可能简单。只要检查新元素并不耗时,只需为每个元素类别使用一个带有自己的 Tick 处理程序的 DispatcherTimer(当然,如果您不这样做)有数百个类别)。

如果检查操作很耗时,它将阻塞 Tick 处理程序,从而阻塞 UI 线程。然后,您可以每使用一个 System.Timers.Timer元素类别并在计时器的 Elapsed 处理程序(而不是 Tick)中执行新元素检查。由于计时器在不同的线程(来自线程池)上运行,因此您必须与 UI 线程同步:

private void VideosTimerElapsed(object sender, ElapsedEventArgs e)
{
    // check for new videos...
    Dispatcher.BeginInvoke(new Action(UpdateUI));
}

private void UpdateUI()
{
    // update UI components
}

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 own Tick 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's Elapsed handler (instead of Tick). Since the timers run on different threads (from the thread pool) you would have to synchronize with the UI thread:

private void VideosTimerElapsed(object sender, ElapsedEventArgs e)
{
    // check for new videos...
    Dispatcher.BeginInvoke(new Action(UpdateUI));
}

private void UpdateUI()
{
    // update UI components
}
情话墙 2025-01-13 13:12:38

默认情况下,WPF 应用程序具有单线程 - 主 UI 线程,所有 UI 控件/窗口均被创建并与其关联。在这种情况下,我没有看到使用多个调度程序和调度程序计时器有任何好处,因为无论如何调度程序计时器会将所有消息委托给关联的调度程序的消息循环(主 UI 线程消息循环)。

但是,如果您在单独的工作线程中创建了一些控件,例如

var thread = new Thread(() => 
         {  
              CustomWindow wnd = new CustomWindow();  
         };

并且也会将消息发布到此窗口 - 那么创建一个新的调度程序并与手动创建的线程关联是有意义的,因此基本上每个调度程序都将与自己的线程关联,所以关系是 1 比 1。

关于 atick 方法 - 它将在与 Dispatcher 线程关联的线程上执行。如果您尚未创建与手动创建的工作线程关联的调度程序,那么默认情况下 WPF 调度程序与主 UI 线程关联,

MSDN:

使用 DispatcherTimer 而不是 System.Timers.Timer 的原因
DispatcherTimer 与 Dispatcher 在同一线程上运行
并且可以在 DispatcherTimer 上设置 DispatcherPriority

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

var thread = new Thread(() => 
         {  
              CustomWindow wnd = new CustomWindow();  
         };

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:

Reasons for using a DispatcherTimer opposed to a System.Timers.Timer
are that the DispatcherTimer runs on the same thread as the Dispatcher
and a DispatcherPriority can be set on the DispatcherTimer

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