如何随时更改时间观察时间?我通过每秒植入时间检查来尝试简单的事情,但这会减慢程序并不断挂断按钮。如何在不挂断程序的情况下完成这项工作?
答案 0 :(得分:1)
您好,您可以使用调度程序计时器。以下是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++;
}
}
}
}