Difference between DispatcherTimer and DispatcherQueueTimer #4770
-
Which is the difference between DispatcherTimer and DispatcherQueueTimer? Reading the docs, it seems they are different api to obtain the same result (I.e. running code on a specified dispatcherqueue/thread. If so, why this duplication? My concrete use case is the simplest one: running few lines on the UI thread. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Essentially, it is historical and on the UWP side where the Xaml interface was copied from. Basically, before the DispatcherQueue was added to Windows, the only dispatcher available for UWP applications was the CoreDispatcher. Back then, DispatcherQueueTimer also didn't exist. I hope it is obvious why. The only UI thread timer available was the DispatcherTimer. Now, when reimplementing Xaml for the desktop environment, changes had to be made to work with that transition. One important difference between the desktop and UWP environments is that the desktop environment doesn't have a CoreDispatcher running. Only a DispatcherQueue is available, which was also reimplemented in Microsoft.UI.Dispatching. So what does this mean for the DispatcherTimer? Well, in order to ease porting, it was reimplemented but to depend on the DispatcherQueue. As to which one to use, they should be pretty similar, I'm sure DispatcherTimer is essentially an alias of DispatcherQueueTimer, where the "constructor" would basically call DispatcherQueue.GetForCurrentThread().CreateTimer(). However, the DispatcherQueueTimer is more flexible, so that would be a better option. |
Beta Was this translation helpful? Give feedback.
Essentially, it is historical and on the UWP side where the Xaml interface was copied from.
Basically, before the DispatcherQueue was added to Windows, the only dispatcher available for UWP applications was the CoreDispatcher. Back then, DispatcherQueueTimer also didn't exist. I hope it is obvious why. The only UI thread timer available was the DispatcherTimer.
Now, when reimplementing Xaml for the desktop environment, changes had to be made to work with that transition. One important difference between the desktop and UWP environments is that the desktop environment doesn't have a CoreDispatcher running. Only a DispatcherQueue is available, which was also reimplemented in Microsoft.UI.Di…