Using the wrong timer in WinUI 3
See the question and my original answer on StackOverflowA timer from System.Timers
will tick on a non-UI thread, so you must use a Microsoft.UI.Dispatching.DispatcherQueue somehow.
You can use the one on the current Window or get one from the current thread (if it has a dispatcher queue) using the DispatcherQueue.GetForCurrentThread method.
Then your code can be written like this:
private void Timer_Tick(object sender, EventArgs e) => DispatcherQueue.TryEnqueue(() =>
{
timerLabel.Text = DateTime.Now.Subtract(startTime).ToString(@"hh\:mm\:ss");
});
Note that you can also use a timer provided by the DispatcherQueue itself with the DispatcherQueue.CreateTimer Method which will automatically tick in the proper thread.