Async event handler Task.Run() vs. normal event handler Task.Run()?

What would be the difference between these two event handlers, one with async, the other without:
private async void ToolStripPlay_Click(object sender, EventArgs e)
{
    var task = Task.Run(() => this.Play()).ConfigureAwait(continueOnCapturedContext: false);
    await task;
}

Open in new window

private void ToolStripPlay_Click(object sender, EventArgs e)
{
    var task = Task.Run(() => this.Play()).ConfigureAwait(continueOnCapturedContext: false);
}

Open in new window

Is there any benefit to using async in this case?
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Is there any benefit to using async in this case?

As it's written, NO.

...but, if you hard more code after the "await", then it would make a huge difference:
private async void ToolStripPlay_Click(object sender, EventArgs e)
{
    var task = Task.Run(() => this.Play()).ConfigureAwait(continueOnCapturedContext: false);
    await task;

    // ... more code here ...

}

Open in new window


With the above code, the GUI would remain responsive while "suspending" at the await line, only continuing after the Task is complete.

Without async, you would block the GUI thread if you waited for the Task to finish.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dirk StraussSenior Full Stack DeveloperCommented:
Download the free ebook Asynchronous Programming Succinctly from Syncfusions website. It's just enough to get a person going with asynchronous programming.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.