2
\$\begingroup\$

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);
Zer0
3532 silver badges9 bronze badges
asked Mar 13, 2020 at 13:00
\$\endgroup\$
1
  • \$\begingroup\$ Is using a timer an option ? \$\endgroup\$ Commented Mar 17, 2020 at 20:11

1 Answer 1

4
\$\begingroup\$

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);
 }
}
answered Mar 17, 2020 at 16:59
\$\endgroup\$
1
  • \$\begingroup\$ You should add in the missing closing parenthesis for BeginInvoke. \$\endgroup\$ Commented Mar 24, 2020 at 2:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.