How to Implement a timer in Metro Style App

2019-04-10 16:16发布

问题:

I am developing a game in which I have to implement timer for showing the time that has elapsed. How can I do that?

回答1:

The async/await way:

private bool isTimerRunning;

private async void StartTimer()
{
    this.isTimerRunning = true;

    while (this.isTimerRunning)
    {
        OnOneSecondPassed();
        await Task.Delay(1000);
    }
}

private void StopTimer()
{
    this.isTimerRunning = false;
}

protected virtual void OnOneSecondPassed()
{
}


回答2:

Did you try to use DispatcherTimer in namespace Windows.UI.Xaml?

If this Timer doesn't fit to your needs, please describe why and what your requirements are.