Short and sweet...
I wrote a cross-thread method that displays the countdown [in seconds] of a delay on a label.
I'm fairly confident it's far from optimal, so I'm in need of that glorious optimization advice.
private async Task SnoozeAsync(int seconds)
{
for (var i = 0; i < seconds; i++)
{
Invoke((MethodInvoker)(() => statusLabel.Text = $"Waiting {seconds - i} seconds..."));
await Task.Delay(1000);
}
}
await SnoozeAsync(60);
-
\$\begingroup\$ Is using a timer an option ? \$\endgroup\$Kate– Kate2020年03月17日 20:11:38 +00:00Commented Mar 17, 2020 at 20:11
1 Answer 1
Invoke
is a blocking call that returns only after that call has competed.
That means your loop is also including the time it takes to marshal over to the GUI thread and complete. You probably don't want that.
I would use BeginInvoke
instead, which does not wait for the method to complete on the GUI thread.
This is also the difference between SynchronizationContext
methods Post
and Send
.
I would also prevent await
from potentially capturing the current SynchronizationContext
using ConfigureAwait(false)
.
To protect against exceptions if the control is disposed of (happens on form close and for other reasons) I'd add an IsDisposed
check.
Finally I would allow this Task
to be cancelled as a matter of best practices using a CancellationToken
.
private async Task SnoozeAsync(int seconds, CancellationToken token)
{
for (var i = 0; i < seconds; i++)
{
if (token.IsCancellationRequested)
break;
BeginInvoke((MethodInvoker)(() =>
{
if (!statusLabel.IsDisposed)
statusLabel.Text = $"Waiting {seconds - i} seconds...";
}));
await Task.Delay(1000, token).ConfigureAwait(false);
}
}
-
\$\begingroup\$ You should add in the missing closing parenthesis for BeginInvoke. \$\endgroup\$Owen– Owen2020年03月24日 02:46:28 +00:00Commented Mar 24, 2020 at 2:46
Explore related questions
See similar questions with these tags.