[MVVMbasics] Using the TimerService

All blog posts with a title that starts with [MVVMbasics] are related to the MVVMbasics framework. If you are only interested in my regular blog posts and not in MVVMbasics, ignore these. To list all articles covering MVVMbasics, go to http://mvvmbasics.mobilemotion.eu/documentation!

Version 1.3 of the MVVMbasics framework features the new TimerService class that is available for all three platforms (Desktop WPF, Phone, and Windows Store / Tablet). In this article, I’d like to show how to create timer events using this new service.

Principles:

The TimerService offers the possibility to create several timers that may run at the same time, each of which having its own tick interval. In general, there are two options when creating a new timer object:

  • Single timers run once, fire some action when the specified interval time has elapsed, and are deleted afterwards.
  • Looping timers are started once, fire some action when the specified interval time has elapsed, and then are started again.

This means that looping timers must be explicitly stopped by the developer if they shall quit sending tick events, while single timers may only be stopped as long as the interval time has not elapsed (because afterwards they are deleted automatically).

Coding:

To create a new timer or stop an existing one, retrieve the TimerService instance through the ServiceLocator. There exist two similar methods for creating and starting timers: StartOnce creates a single timer, while StartLooping creates a looping timer. It is important to notice that there are no explicit Create and Start methods, instead the two Start... methods create and at the same time start a new timer.

Both Start... methods mentioned above take the desired interval (as TimeSpan object) and a callback action as parameters. As the callback action, pass a method that shall be executed on each tick event. In addition, both Start... methods return a GUID identifier that can be used later to stop specific timers. When defining only one timer in your application, it’s not necessary to store this identifier, otherwise it’s recommended to store it within a local Guid variable.

In addition, the TimerService instance offers a Stop method that can be used to stop looping timers and single timers that have not yet been elapsed. This method can be called in several ways:

  • If there is only one timer defined in the App, to stop this one timer you can simply call the Stop method without parameters.
  • If there are multiple timers defined in the App that allshall be stopped, you can also just call the Stop method without parameters – it will stop all timers that have been created and have not yet elapsed.
  • To stop a specific timer, call the Stop method and pass the desired target timer’s GUID identifier as parameter.
  • To stop several specific timers at the same time, you might also pass several GUID identifiers as parameters to the Stop method.

The detailed specification of the ITimerService interface and all the platform-specific TimerService classes can be found in the MVVMbasics Class Reference, on the pages ITimerService, MVVMbasicsPhoneExtensions.TimerService, MVVMbasicsTabletExtensions.TimerService, and MVVMbasicsDesktopExtensions.TimerService.

Important hints:

When creating timer objects using the TimerService, these timers are bound to the App, not to a single View (page or window). In practive however, timers often will be used to trigger UI changes and are therefore only useful within special Views: When exiting the current View, these timers should be stopped since the UI event they are about to trigger is not necessary any more, except for when the View is only moved to the background and might be reloaded later. The TimerService does not handle these scenarios – for each timer, you need to decide on your own whether this timer affects View-specific UI elements, and in that case manuallly stop them in the Viewmodel’s OnNavigatedFrom event handler method!

What’s behind?

Technically speaking, the TimerService is a wrapper of the platform-specific DispatcherTimer classes, meaning that it is the method of choice when defining timer events that update the UI.