DispatcherTimer VS定期定时在WPF应用程序的任务调度 [英] DispatcherTimer vs a regular Timer in WPF app for a task scheduler

查看:833
本文介绍了DispatcherTimer VS定期定时在WPF应用程序的任务调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,解释@Kent Boogaart意味着在多线程WPF应用程序使用作为一项任务sheduler本主题中DispatcherTimer和定期定时的区别:

Please, explain the difference between "DispatcherTimer" and "a regular Timer" that @Kent Boogaart meant for using in a multithreading WPF app as a task sheduler in this topic:

<一个href="http://stackoverflow.com/questions/1341829/advice-needed-for-multi-threading-strategy-for-wpf-application">http://stackoverflow.com/questions/1341829/advice-needed-for-multi-threading-strategy-for-wpf-application

在评注的后一(报价):

in the commentaries to one of the post (quote):

- 如果所有的DispatcherTimer确实被揭开序幕另一个线程,什么是使用DispatcherTimer的地步? ....那些线程不需要被UI线程上启动。你可以只用一个普通定时器和完全避免中断UI

-If all the DispatcherTimer does is kick off another thread, what is the point of using the DispatcherTimer? ....those threads don't need to be started on the UI thread. You could just use a regular Timer and avoid interrupting the UI altogether

什么是定期定时是指?他们是如何(DispatcherTimer和定期定时)不同,就其对UI?影响

(直到看完这帖子我想到DispatcherTimer如使用WPF计时器自然的方式,什么情况下,当这是不是真的?)

(Until reading this post I thought about DispatcherTimer as a natural way of using timers in WPF. What is the cases when this is not true?)

推荐答案

DispatcherTimer是定期计时器。它激发其Tick事件在UI线程上,你可以做你想做的任何用户界面。 System.Timers.Timer的是一个异步的计时器,其经过的事件在一个线程池线程运行。你必须非常小心,在你的事件处理程序,不准接触任何UI组件和数据绑定变量。你将需要使用在任何你访问类成员也被用来在UI线程锁声明。

DispatcherTimer is the regular timer. It fires its Tick event on the UI thread, you can do anything you want with the UI. System.Timers.Timer is an asynchronous timer, its Elapsed event runs on a thread pool thread. You have to be very careful in your event handler, you are not allowed to touch any UI component or data-bound variables. And you'll need to use the lock statement where ever you access class members that are also used on the UI thread.

在连接答案,Timer类是比较合适的,因为OP试图运行code的目的是异步的。

In the linked answer, the Timer class was more appropriate because the OP was trying to run code asynchronously on purpose.

这篇关于DispatcherTimer VS定期定时在WPF应用程序的任务调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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