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);
\$\endgroup\$
1
  • \$\begingroup\$ Is using a timer an option ? \$\endgroup\$
    – Kate
    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);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ You should add in the missing closing parenthesis for BeginInvoke. \$\endgroup\$
    – Owen
    Commented Mar 24, 2020 at 2:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.