I found [this article] that shows how to build a stopwatch / timer and after about 15 minutes there's 5 second gap between my physical timer (good battery; physical timer goes ahead 5 seconds)
async Task StartTimer()
{
SetButtonState();
isStopWatchRunning = true;
while (isStopWatchRunning)
{
await Task.Delay(1000);
if (isStopWatchRunning)
{
stopWatchValue = stopWatchValue.Add(new TimeSpan(0, 0, 1));
StateHasChanged();
}
}
}
You are not measuring and accounting for how long your other code takes to run such as the call to StateHasChanged() and it is called ~900 times in that 15 minutes. Also, Task.Delay is not guaranteed to be accurate as stated in the responses to the StackOverflow question about its accuracy in the link below; which also has some possible solutions.
Accuracy of Task.Delay