Does anyone know of example code to illustrate the creation of a periodic timer, say with DispatcherTimer, in c++/winrt? The examples in the docs are managed C++ and I have not been able to successfully convert them for use with c++/winrt. Thanks... [Update: in response to popular demand, let me show my own attempts to translate the C++/CX code. Here is the sample code from the DispatcherTimer documentation:
void MainPage::StartTimerAndRegisterHandler() {
auto timer = ref new Windows::UI::Xaml::DispatcherTimer();
TimeSpan ts;
ts.Duration = 500;
timer->Interval = ts;
timer->Start();
auto registrationtoken = timer->Tick += ref new EventHandler<Object^>(this, &MainPage::OnTick);}
void MainPage::OnTick(Object^ sender, Object^ e) {
// do something on each tick here ...}
//Now, for translating to C++/winrt:
void MainPage::StartTimerAndRegisterHandler() {
auto timer = Windows::UI::Xaml::DispatcherTimer(); //that's easy enough
TimeSpan ts = TimeSpan(500); //This can be done in one step, I think
timer.Interval(ts); //And this is easy
timer.Start(); //Seems right
//The following line is the tricky bit.
//I change timer->Tick to timer.Tick
//The following += is described as a way to add a delegate; I drop the ref new
//But Object is not going to work here; it needs to be replaced
//by, I think, IInspectable const & sender, and probably a lambda
//would replace the OnTick, there is a lot of mystery on this line
//and there hardly seems any point in displaying my several attempts to get past it:
auto registrationtoken = timer.Tick += ref new EventHandler<Object^>(this, &MainPage::OnTick);}
So, if anyone has a way to implement that tick handler in cppwinrt I would love to see it. Thanks.
It appears that you are having difficulty registering for a custom event. While C++/CX uses a compact syntax already, C++/WinRT further reduces the amount of code required to set up an event handler.
The DispatcherTimer
's Tick event handler receives an IInspectable
parameter. The signature is:
void MainPage::OnTick(IInspectable const& sender, IInspectable const& event)
Registering the event handler with the Tick
event is fairly straight-forward as well. Although you can name the EventHandler class template type of the temporary, there is no reason to do so. Using uniform initialization syntax, it becomes as simple as calling
auto registrationtoken = timer.Tick({ this, &MainPage::OnTick });
The code for unregistering the event handler looks like this:
timer.Tick(registrationtoken);
This looks remarkably similar to the registration code, which I find a bit unfortunate. It'll take a while for your eyes get trained to identify either one.
Note, that while you can use unnamed lambdas with custom event handlers, it is discouraged so as to not introduce circular references.
There is additional information in the official (yet unmaintained) GitHub repository for C++/WinRT: Registering for events using a lambda. There is also official documentation in the MSDN: Events; how to author and handle them in C++/WinRT.