Before reading this article, I highly recommend reading the previous parts of the series.
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).
- DispatcherTimer dispatchTimer;
- std::chrono::seconds sec(1);
- dispatchTimer.Interval(sec);
- dispatchTimer.Start();
Tick
Occurs when the timer interval has elapsed (based on the interval set by the user).
- DispatcherTimer dispatchTimer;
-
- dispatchTimer.Tick({ this,&App::dispatch_tick });
-
- void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)
- {
- }
Start
Start function is used to start the Timer. If the timer is already running, call the start method because it restarts the process.
- DispatcherTimer dispatchTimer;
- dispatchTimer.Start();
Stop
Stop function is used to stop the time if it is running.
- DispatcherTimer dispatchTimer;
- dispatchTimer.Stop();
Let us see one example.
Call the DispatcherTimer every second and automatically increase the ProgressBar value.
- #include "pch.h"
-
- using namespace winrt;
- using namespace Windows::ApplicationModel;
- using namespace Windows::ApplicationModel::Activation;
- using namespace Windows::Foundation;
- using namespace Windows::UI;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- using namespace Windows::UI::Xaml::Controls::Primitives;
- using namespace Windows::UI::Xaml::Interop;
- using namespace Windows::UI::Xaml::Navigation;
- using namespace Windows::UI::Xaml::Media;
- using namespace Windows::Media;
- using namespace Windows::Media::Core;
- using namespace Windows::Storage;
- using namespace Windows::Storage::Pickers;
-
-
- struct App :ApplicationT<App>
- {
- TextBlock txtStatus;
- DispatcherTimer dispatchTimer;
- ProgressBar pBar;
- int value = 20;
-
- IInspectable CreateInspectable(hstring strCaption)
- {
- return PropertyValue::CreateString(strCaption);
- }
-
- void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)
- {
-
- auto CtlValue = pBar.Value();
-
- CtlValue += 10;
-
- if (CtlValue > 100)
- CtlValue = 0;
- else if (CtlValue <= 0)
- CtlValue += 10;
-
- if (CtlValue <= 40)
- pBar.Foreground(SolidColorBrush(Colors::Red()));
-
- else if (CtlValue <= 80)
- pBar.Foreground(SolidColorBrush(Colors::Yellow()));
- else if (CtlValue <= 100)
- pBar.Foreground(SolidColorBrush(Colors::Green()));
-
- pBar.Value(CtlValue);
-
- }
-
- TextBlock CreateTextBlock(hstring text)
- {
- TextBlock txtBlock;
- txtBlock.Text(text);
- txtBlock.FontFamily(FontFamily(L"Segoe UI Semibold"));
- txtBlock.TextAlignment(TextAlignment::Left);
- txtBlock.FontSize(35);
- txtBlock.Foreground(SolidColorBrush(Colors::Brown()));
- txtBlock.Margin(CreateThickness(10, 10, 0, 0));
- return txtBlock;
- }
-
- Thickness CreateThickness(int left, int top, int right, int bottom)
- {
- Thickness thick;
- thick.Left = left;
- thick.Top = top;
- thick.Right = right;
- thick.Bottom = bottom;
- return thick;
- }
-
-
- void OnLaunched(LaunchActivatedEventArgs const&)
- {
- auto txtheader = CreateTextBlock(L"DispatcherTimer Sample(Modern C++/WinRT)");
- txtheader.FontSize(25);
-
- StackPanel btnPanel;
- btnPanel.Orientation(Orientation::Horizontal);
- btnPanel.Children().Append(BtnIncrease);
- btnPanel.Children().Append(BtnDecrease);
-
-
- StackPanel sContentPanel;
- sContentPanel.Orientation(Orientation::Vertical);
- sContentPanel.VerticalAlignment(VerticalAlignment::Top);
-
- dispatchTimer.Tick({ this,&App::dispatch_tick });
-
- std::chrono::seconds sec(1);
- dispatchTimer.Interval(sec);
- dispatchTimer.Start();
-
-
- pBar.Minimum(0);
- pBar.Maximum(100);
- pBar.Value(value);
- pBar.Height(100);
- pBar.Margin(CreateThickness(5, 20, 10, 0));
- pBar.IsIndeterminate(false);
- pBar.Foreground(SolidColorBrush(Colors::Red()));
-
- StackPanel mainPanel;
- mainPanel.Children().Append(sContentPanel);
- mainPanel.Children().Append(txtheader);
- mainPanel.Children().Append(pBar);
-
-
-
- Window window = Window::Current();
- window.Content(mainPanel);
- window.Activate();
-
-
-
-
- }
- };
-
- int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
- {
- Application::Start([](auto &&) {make<App>(); });
- return 0;
- }
Output
Conclusion
I hope, you understood how to use the DispatcherTimer control.