Timer 与 DispatcherTimer 的性能

发布于 2024-12-19 10:48:10 字数 518 浏览 1 评论 0原文

我心里没有具体的场景,但当我考虑可能想要使用 Timer 而不是 DispatcherTimer 的场景时,这个问题突然出现在我的脑海中。

在每当计时器事件触发时我都必须执行计算密集型任务,然后对 UI 进行微小修改的情况下,在性能方面会更好:

  1. 使用常规计时器,然后使用应用程序的调度程序 更改 UI
  2. 使用 DispatcherTimer (并且可能通过计算来完成我的操作) 如果需要的话,在一些异步后台工作人员中进行密集的工作)。

我的猜测是,尽可能长时间地保持 UI 线程不阻塞将增强用户体验。如果这是可取的,那么在这种情况下我应该注意什么问题吗?

编辑:

我感觉我的问题不够清楚,所以我将尝试添加一个具体的(尽管是虚构的)示例。

假设我必须每 2 分钟读取一个大文件,完成后,我必须向 ListBox 添加一个项目。假设读取/处理文件需要 10-15 秒,在此期间我不执行 UI 工作。对于这样的事情最好的方法是什么?

I don't have a specific scenario in mind, but this question just crossed my mind while I was thinking of scenarios where I may want to use a Timer over a DispatcherTimer.

In the scenario where I have to perform come computationally intensive task whenever a timer event fires, and then make minor modifications to UI, would it be better in terms of performance to:

  1. use a regular Timer and then use the application's Dispatcher to
    change the UI
  2. use a DispatcherTimer (and possibly do my computationally
    intensive work in some async background worker if necessary).

My guess is that keeping the UI thread unblocked for as long as possible will enhance the user experience. If this is advisable, Are there any catches that I should be aware of in such a scenario?

EDIT:

I get the feeling my question was not clear enough, so I'm going to try and add a concrete, albeit made-up example.

Let's say I have to read a large file every 2 minutes, and when I'm done, I have to add an item to a ListBox. Let's say reading/processing the file takes 10-15 seconds, during which I do no UI work. What would be the best approach for something like this?

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

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

发布评论

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

评论(6

写下不归期 2024-12-26 10:48:10

要点:

执行计算密集型任务

这表明使用DispatcherTimer。它的存在主要是为了在主线程上执行小任务并避免创建另一个线程。

如果您使用 DispatcherTimer 来启动后台工作程序,那么您就绕过了它的主要目的。

所以这里只使用常规的定时器。

Main point:

to perform come computationally intensive task

This would indicate not using the DispatcherTimer. It mainly exists to perform small tasks on the main thread and avoid creating another thread.

If you use a DispatcherTimer to start a Backgroundworker you're circumventing its main purpose.

So just use a regular Timer here.

呆头 2024-12-26 10:48:10

经过更多的研究和分析,我得出的结论是,在我编造的示例中,常规计时器效果最好。到目前为止,我还没有必要寻找任何可能导致我的代码出现潜在问题的具体内容。话虽如此,良好的编码实践永远不会有坏处!

After some more research and analysis, I've come to the conclusion that a regular timer works best in my made-up example. So far, I haven't had to look out for anything specific that would cause potential problems in my code. Having said that, good coding practices never hurt!

山川志 2024-12-26 10:48:10
  • Timer 在应用程序中生成重复事件

  • DispatcherTimer 是集成到 Dispatcher 中的计时器
    以指定的时间间隔和时间处理的队列
    指定优先级。

定时器不保证在时间间隔发生时准确执行,但保证不在时间间隔发生之前执行。这是因为 DispatcherTimer 操作像其他操作一样被放置在 Dispatcher 队列中。当 DispatcherTimer 操作执行时,它取决于队列中的其他作业及其优先级。

如果在 WPF 应用程序中使用计时器,则值得注意的是,计时器在与用户界面 (UI) 线程不同的线程上运行。为了访问用户界面 (UI) 线程上的对象,需要使用 Invoke 或 BeginInvoke 将操作发布到用户界面 (UI) 线程的 Dispatcher 上。使用 DispatcherTimer 而不是 Timer 的原因是 DispatcherTimer 与 Dispatcher 运行在同一线程上,并且可以设置 DispatcherPriority。

  • Timer generates recurring events in an application

  • DispatcherTimer is a timer that is integrated into the Dispatcher
    queue which is processed at a specified interval of time and at a
    specified priority.

Timers are not guaranteed to execute exactly when the time interval occurs, but are guaranteed not to execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes, it is dependent of the other jobs in the queue and their priorities.

If a Timer is used in a WPF application, it is worth noting that the Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set.

姜生凉生 2024-12-26 10:48:10

比较 Timer 与 DispatcherTimer
这里发布的一些答案更具体地针对您的问题,但我认为此链接提供了一些一般信息和建议。

Comparing Timer with DispatcherTimer
Some answers posted here are more specific to your question but I think this link offers some general information and advice.

探春 2024-12-26 10:48:10

如果您想更改绑定到 UI 元素的标量(不是集合)值,您不仅可以从 UI 线程(例如从 Timer 委托)执行此操作。在这种情况下,您不需要使用 Dispatcher.Invoke/BeginInvoke。

If you want to change a scalar (not a collection) value binded to UI element, you can do it not only from UI thread (e.g. from Timer delegate). And in this case you don't need to use Dispatcher.Invoke/BeginInvoke.

开始看清了 2024-12-26 10:48:10

另一种选择是使用 DispatcherTimer 在每次计时器迭代上创建 BackgroundWorker 类实例。在许多情况下,它还使您免于使用 Dispatcher.Invoke/BeginInvoke。

Another option is to use DispatcherTimer with creating BackgroundWorker class instance on each timer iteration. It also frees you from using Dispatcher.Invoke/BeginInvoke in many cases.

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