C# async event handler vs. normal event handler?

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

Open in new window

as opposed to
private void ToolStripPlay_Click(object sender, EventArgs e)
{
     this.Play();
}

Open in new window

What is the advantage of assigning Play() to a Task and then waiting for that Task to complete, as opposed to just running the task?

I see the Task will run on it's own thread, instead of the UI thread, but it looks like the UI thread waits for the Task to complete before returning. (Play() sets the state to Play mode and returns.)
deleydAsked:
Who is Participating?
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.

AndyAinscowFreelance programmer / ConsultantCommented:
I don't see any real difference once the event handler is called.  Both block the UI thread until the task is completed.
0
Jayadev NairApplication DeveloperCommented:
If play() method is a long running process and by suspending main thread, we should see a potential difference by letting the UI paint events happen.
0
Chris StanyonWebDevCommented:
When you call await, your program returns to the calling method and so can continue to run (freeing up the UI). It will pick up where it left off once the task has finished. By not calling await, you program will halt at the play call and not return to the calling method until it's finished, therefore blocking the UI.
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
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
C#

From novice to tech pro — start learning today.

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.