Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

.net - DispatcherTimer vs a regular Timer in WPF app for a task scheduler

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:

Advice needed for multi-threading strategy for WPF application

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

-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

What is "a regular timer" that is meant? How they ("DispatcherTimer" and "a regular Timer") differ regarding their impact on UI?

(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?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...