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.