In making the switch from Windows forms to WPF you may come across a situation where you want to do a certain action at a specified interval of time. In Windows Forms, the Timer class handled this situation for you, and in fact, if you really wanted to you could import the namespace and use the same class in your Windows Forms application – but this is not ideal, as the Timer class is optimized for use in a Windows Forms Window.
So, instead, use the System.Windows.Threading.DispatcherTimer class, which can perform a similar duty, but is optimized for use with WPF. It runs on the same thread as it’s dispatcher, so it is optimized for updating the UI – if you have large amounts of work to do, you should use a separate thread, or the BackgroundWorker class.
The two classes are very similar, though one difference to be aware of is that the Timer class’s Interval property is measured in milliseconds, where the DispatcherTimer uses an actual Timespan object, which measures time in ticks. Make sure you keep that in mind otherwise your actions may be performed more regularly than you expected.
To read more about how to use the DispatcherTimer and BackgroundWorker classes to build responsive WPF applications, check out this MSDN article.
1 comment:
DispatcherTimer is appropriate for planned projects. We can set the holding out crack according to actual specifications.
BackgroundWorker is fairly appropriate for starting a single, asynchronous, and long process that runs in the background.
Post a Comment