Reputation: 11
I have this method that i need to pause for some period of time. This is what I tried:
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
this.Dispatcher.Invoke((Action)(() =>
{
NewGame();
// Game Loop
for (int i = 0; i < 35; i++)
{
int index = _random.Next(0, _numbersInGame.Count);
int number = _numbersInGame.ToArray()[index];
lblCurrentNumber.Content = number.ToString();
Task.Delay(1000).Wait(); // I want to add pause here
(this.FindName($"lblNum{i + 1}") as Label).Content = number.ToString();
lblCurrentNumber.Content = "";
_numbersInGame.RemoveAt(index);
}
}));
});
}
Any help is welcome. Thanks!
Upvotes: 0
Views: 95
Reputation: 81483
Task.Run
)Invoke
.await
a Task.Delay
without blocking the UIExample
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 35; i++)
{
NewGame();
int index = _random.Next(0, _numbersInGame.Count);
int number = _numbersInGame.ToArray()[index];
lblCurrentNumber.Content = number.ToString();
await Task.Delay(100);
(this.FindName($"lblNum{i + 1}") as Label).Content = number.ToString();
lblCurrentNumber.Content = "";
_numbersInGame.RemoveAt(index);
}
}
Using the async and await pattern like this allows you to safely update the UI as the continuation will be posted back to the UI context. It also allows the Message Pump / Dispatcher to continue unabated until the awaited delay has completed.
Lastly, you might want to protect this method from double clicks with a flag or similar (since the ui is free to reenter)
Upvotes: 2