比较 Timer 和 DispatcherTimer [英] Comparing Timer with DispatcherTimer

查看:16
本文介绍了比较 Timer 和 DispatcherTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Windows.Forms.Timer()System.Windows.Threading.DispatcherTimer() 有什么区别?在哪些情况下,我们应该使用它们?有什么最佳做法吗?

what is a difference between System.Windows.Forms.Timer() and System.Windows.Threading.DispatcherTimer() ? In which cases, we should use them? any best practices ?

推荐答案

Windows.Forms.Timer 使用 windows 窗体消息循环来处理计时器事件.当编写在 Windows 窗体应用程序中使用的计时事件并且您希望计时器在主 UI 线程上触发时,应该使用它.

Windows.Forms.Timer uses the windows forms message loop to process timer events. It should be used when writing timing events that are being used in Windows Forms applications, and you want the timer to fire on the main UI thread.

DispatcherTimer 是 WPF 计时机制.当您想以类似的方式处理计时(尽管这不限于单个线程 - 每个线程都有自己的调度程序)并且您正在使用 WPF 时,应该使用它.它在与 Dispatcher 相同的线程上触发事件.

DispatcherTimer is the WPF timing mechanism. It should be used when you want to handle timing in a similar manner (although this isn't limited to a single thread - each thread has its own dispatcher) and you're using WPF. It fires the event on the same thread as the Dispatcher.

一般来说,WPF == DispatcherTimerWindows Forms == Forms.Timer.

话虽如此,还有系统.Threading.Timer,它是一个在单独线程上触发的计时器 class.这对于纯粹的数字计时很有用,您不会尝试更新 UI 等.

That being said, there is also System.Threading.Timer, which is a timer class that fires on a separate thread. This is good for purely numerical timing, where you're not trying to update the UI, etc.

这篇关于比较 Timer 和 DispatcherTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆