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.