Learn Universal Windows Programming Via Modern C++ (DispatcherTimer)

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.   
  3. dispatchTimer.Tick({ this,&App::dispatch_tick });  
  4.   
  5. void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)  
  6. {  
  7. }  

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

 

Conclusion

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


Similar Articles