0

How do I observe the time regularly as it changes? I tried the simple thing by implanting time check at every second but that slowed the program and constantly hung up the buttons. How do I do it without hanging up the program?

Gaurab
  • 1
  • 7
  • Can you post the relevant part of your code? – Abdullah Nehir Jun 03 '16 at 06:32
  • By executing the check in another thread. – Allmighty Jun 03 '16 at 06:35
  • What do you mean by observing time: You need to know how much time has passed since something? You need to perform an operation every X? You need to update something when something happens asap? – 3615 Jun 03 '16 at 06:37
  • @3615 its like I have to perform an operation every X. – Gaurab Jun 03 '16 at 06:44
  • 1
    Then you would need to use a **Timer** (not sure which one to use for UWP). Timer allows you to execute operation every X. Check this answer to see an [example](http://stackoverflow.com/a/34271284/5246145) @Gaurab – 3615 Jun 03 '16 at 06:48

1 Answers1

1

Hello you can use dispatcher timer. Here is an example implementation in UWP:

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer timer;
        private int counter = 0;

        public MainPage()
        {
            this.InitializeComponent();
            timer = new DispatcherTimer();

            Loaded += MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Interval = TimeSpan.FromSeconds(1.0);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, object e)
        {
            if (counter == 10)
            {
                //do operation in every 10 seconds
                counter = 0;
                //if you want to stop the timer use timer.Start()
            }
            else
            {
                counter++;
            }
        }
    }
}
Hasan Hasanov
  • 1,129
  • 2
  • 15
  • 23