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?
deleydAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.