17

I was doing some coding in ASP.NET when I came across this:

protected async void someButtonClickHandler(...)
{
 if(await blah)
 doSomething();
 else
 doSomethingElse();
}

After asking this question I got a better understanding of how async/await works. But then it struck me is it safe to use async/await in the manner shown above?
I mean after calling await blah the caller continues execution. Which means it might render the response back to client before await blah completes. Is this right? And if this is the case what happens to doSomething()/doSomethingElse(). Will they ever be executed? If they're executed does the user see the effects of their changes?
In my case these methods change some data displayed to the user, but I'd also like to know what would happen in the general case.

asked Dec 3, 2014 at 22:07
1
  • Is this for web forms? Commented Dec 3, 2014 at 22:33

2 Answers 2

28

Yes it is safe, but not really recommended. The recommended way to do this is via RegisterAsyncTask. However, ASP.NET (Web Forms) will correctly handle async void event handlers.

The response is not rendered to the client when the handler awaits; the await only yields to the ASP.NET runtime, not to the client. The ASP.NET runtime is aware that the event handler has not completed, so it knows not to send the response. When the event handler completes, the ASP.NET runtime responds by sending the response at that time.

I have an MSDN article on async ASP.NET that you may find helpful. If you're curious about how the ASP.NET runtime is aware that the async handler has not completed, I cover that in an earlier MSDN article.

answered Dec 3, 2014 at 22:14

2 Comments

your first link is currently broken
@Fredou: The link is correct; it's a problem with the ASP.NET site. Just hit refresh until the content shows up.
0

I agree with Stephens answer (in short the ASP.NET SynchronisationContext keeps watch on how many tasks are running), but because it is a (best avoided) async void you might want to log any exceptions that would otherwise go un-noticed:

protected async void someButtonClickHandler(...)
{
 try{
 await someButtonClickHandlerInner(...)
 }
 catch (AggregateException ex)
 {
 logger.log(ex.flatten());
 }
 catch(Exception e){
 logger.log(e); 
 }
}
private async Task someButtonClickHandlerInner(...){
 if(await blah)
 doSomething();
 else
 doSomethingElse();
}
answered Feb 5, 2021 at 8:00

Comments

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.