Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

UWP How to create a small timer

10.07.2018 (👁4625)

UWP How to create a small timer

06.07.2018 (👁25)

How to create a small timer, which counts from 10 to 0 and then starts a process

 

Solution:

In Windows 10 UWP you use the

DispatcherTimer Timer = new DispatcherTimer();

 

At the beginning, set the timer to the time unit in which it should count.

Here is a timer event per second to be triggered

            Timer.Interval = new TimeSpan(0, 0, 1);

            Timer.Start();

 

Then create an event, which is triggered by the timer-click

Timer.Tick += Timer_Tick;

 

And in the tick event of the timer you write then the counter or other logic

        private void Timer_Tick(object sender, object e)

        {

            //--------< Timer_Tick() >--------

            //*decrease Counter. Start MainRun at 0

            //< decrease > 

            CountDown_Seconds = CountDown_Seconds - 1;

            //</ decrease >

            fx_Log(CountDown_Seconds.ToString()); //show text

 

            //-< run_on_0 >-

            if (CountDown_Seconds == 0)

            {

                Timer.Stop();  //*stop timer

                fx_Run_Main(); //*run main

            }

            //-</ run_on_0 >-

            //--------</ Timer_Tick() >--------

        }

 

 

 

 

Timer, Counter, Autostart, Autocounter

UWP, Windows 10

 

Example code in C #

using System;

 

    public sealed partial class MainPage : Page

    {

        DispatcherTimer Timer = new DispatcherTimer();

        public int CountDown_Seconds = 10;

 

        #region Page

        //--------------------< region: Page >---------------------

        public MainPage()

        {

            this.InitializeComponent();

            Current_Mainpage = this;

 

            optStop.IsChecked = false;

 

            //< start Timer >

            Timer.Tick += Timer_Tick;

            Timer.Interval = new TimeSpan(0, 0, 1);

            Timer.Start();

            //</ start Timer >

        }

 

       //--------------------</ region: Page >---------------------

        #endregion /Page

 

 

 

        #region Controls

        //--------------------< region: Controls >---------------------

        private void optStop_Click(object sender, RoutedEventArgs e)

        {

            Timer.Stop();

        }

        private void Timer_Tick(object sender, object e)

        {

            //--------< Timer_Tick() >--------

            //*decrease Counter. Start MainRun at 0

            //< decrease > 

            CountDown_Seconds = CountDown_Seconds - 1;

            //</ decrease >

            fx_Log(CountDown_Seconds.ToString());

 

            //-< run_on_0 >-

            if (CountDown_Seconds == 0)

            {

                Timer.Stop();  //*stop timer

                fx_Run_Main(); //*run main

            }

            //-</ run_on_0 >-

            //--------</ Timer_Tick() >--------

        }

        //--------------------</ region: Controls >---------------------

        #endregion /Controls

 

 

 

Countdown timer: at 0, the main routine starts

https://readdy.net/User_Files/Notes/Images/Image_568_0_blog.jpg" width="796" height="548">