Before reading this article, I highly recommend reading the previous parts of the series.

DispatcherTimer

DispatcherTimer is a timer control that is integrated into the Dispatcher queue. A Timer event is called in a specific time interval. The main advantage of a DispatcherTimer is that it can run the code on the same UI thread.

Let us see how to implement this feature and important properties.

Interval - How frequently an event should be triggered (i.e. amount of time between ticks)

TimeSpan as an input for the Interval - Actually, it is std::Chrono in C++ ( C++/WinRT is typecast into the TimeSpan type).
Given below is the sample code how to define the TimeSpan ( It's specified as1 second).
  1. DispatcherTimer dispatchTimer;
  2. std::chrono::seconds sec(1);
  3. dispatchTimer.Interval(sec);
  4. dispatchTimer.Start();

Tick

Occurs when the timer interval has elapsed (based on the interval set by the user).

  1. DispatcherTimer dispatchTimer;
  2. dispatchTimer.Tick({ this,&App::dispatch_tick });
  3. void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)
  4. {
  5. }

Start

Start function is used to start the Timer. If the timer is already running, call the start method because it restarts the process.

  1. DispatcherTimer dispatchTimer;
  2. dispatchTimer.Start();

Stop

Stop function is used to stop the time if it is running.

  1. DispatcherTimer dispatchTimer;
  2. dispatchTimer.Stop();
Let us see one example.

Call the DispatcherTimer every second and automatically increase the ProgressBar value.
  1. #include "pch.h"
  2. using namespace winrt;
  3. using namespace Windows::ApplicationModel;
  4. using namespace Windows::ApplicationModel::Activation;
  5. using namespace Windows::Foundation;
  6. using namespace Windows::UI;
  7. using namespace Windows::UI::Xaml;
  8. using namespace Windows::UI::Xaml::Controls;
  9. using namespace Windows::UI::Xaml::Controls::Primitives;
  10. using namespace Windows::UI::Xaml::Interop;
  11. using namespace Windows::UI::Xaml::Navigation;
  12. using namespace Windows::UI::Xaml::Media;
  13. using namespace Windows::Media;
  14. using namespace Windows::Media::Core;
  15. using namespace Windows::Storage;
  16. using namespace Windows::Storage::Pickers;
  17. struct App :ApplicationT<App>
  18. {
  19. TextBlock txtStatus;
  20. DispatcherTimer dispatchTimer;
  21. ProgressBar pBar;
  22. int value = 20;
  23. IInspectable CreateInspectable(hstring strCaption)
  24. {
  25. return PropertyValue::CreateString(strCaption);
  26. }
  27. void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)
  28. {
  29. auto CtlValue = pBar.Value();
  30. CtlValue += 10;
  31. if (CtlValue > 100)
  32. CtlValue = 0;
  33. else if (CtlValue <= 0)
  34. CtlValue += 10;
  35. if (CtlValue <= 40)
  36. pBar.Foreground(SolidColorBrush(Colors::Red()));
  37. else if (CtlValue <= 80)
  38. pBar.Foreground(SolidColorBrush(Colors::Yellow()));
  39. else if (CtlValue <= 100)
  40. pBar.Foreground(SolidColorBrush(Colors::Green()));
  41. pBar.Value(CtlValue);
  42. }
  43. TextBlock CreateTextBlock(hstring text)
  44. {
  45. TextBlock txtBlock;
  46. txtBlock.Text(text);
  47. txtBlock.FontFamily(FontFamily(L"Segoe UI Semibold"));
  48. txtBlock.TextAlignment(TextAlignment::Left);
  49. txtBlock.FontSize(35);
  50. txtBlock.Foreground(SolidColorBrush(Colors::Brown()));
  51. txtBlock.Margin(CreateThickness(10, 10, 0, 0));
  52. return txtBlock;
  53. }
  54. Thickness CreateThickness(int left, int top, int right, int bottom)
  55. {
  56. Thickness thick;
  57. thick.Left = left;
  58. thick.Top = top;
  59. thick.Right = right;
  60. thick.Bottom = bottom;
  61. return thick;
  62. }
  63. void OnLaunched(LaunchActivatedEventArgs const&)
  64. {
  65. auto txtheader = CreateTextBlock(L"DispatcherTimer Sample(Modern C++/WinRT)");
  66. txtheader.FontSize(25);
  67. StackPanel btnPanel;
  68. btnPanel.Orientation(Orientation::Horizontal);
  69. btnPanel.Children().Append(BtnIncrease);
  70. btnPanel.Children().Append(BtnDecrease);
  71. StackPanel sContentPanel;
  72. sContentPanel.Orientation(Orientation::Vertical);
  73. sContentPanel.VerticalAlignment(VerticalAlignment::Top);
  74. dispatchTimer.Tick({ this,&App::dispatch_tick });
  75. std::chrono::seconds sec(1);
  76. dispatchTimer.Interval(sec);
  77. dispatchTimer.Start();
  78. pBar.Minimum(0);
  79. pBar.Maximum(100);
  80. pBar.Value(value);
  81. pBar.Height(100);
  82. pBar.Margin(CreateThickness(5, 20, 10, 0));
  83. pBar.IsIndeterminate(false);
  84. pBar.Foreground(SolidColorBrush(Colors::Red()));
  85. StackPanel mainPanel;
  86. mainPanel.Children().Append(sContentPanel);
  87. mainPanel.Children().Append(txtheader);
  88. mainPanel.Children().Append(pBar);
  89. Window window = Window::Current();
  90. window.Content(mainPanel);
  91. window.Activate();
  92. }
  93. };
  94. int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
  95. {
  96. Application::Start([](auto &&) {make<App>(); });
  97. return 0;
  98. }
Output



Conclusion

I hope, you understood how to use the DispatcherTimer control.