Assuming the `UpdateUIAsync` method is called from a background thread (`Task.Run`), identify the bug in this code.
.NET interview question for Advanced practice.
Answer
The UI update is attempted from a thread pool thread, leading to a cross-thread exception. The fix is to marshal the update to the UI thread (e.g., using Dispatcher.Invoke).
Explanation
Because the method was started on a background thread, there is no UI SynchronizationContext to capture. After Task.Delay completes, the continuation (myLabel.Text = ...) will also execute on a thread pool thread. Attempting to access and modify a UI element (myLabel) from a non-UI thread will result in a runtime exception (e.g., InvalidOperationException).