4

I generally program webservers and at first I thought that there must be continuous chain of methods that return task, so stuff up the stack may ask database if it is done.

Recently I saw wpf code, that does something like that:

    public async void Execute(object parameter)
    {
        await ExecuteAsync(parameter);
    }

Called in event handler. UI seems to be responsive, so I guess it does work. How does it work? How does this translate to aspnet?

0

2 Answers 2

11

I explain how async void methods work - and why they should be avoided - in my Best Practices in Asynchronous Programming article.

async void has the same semantics as async Task, except for exceptions. An async void method will capture the current SynchronizationContext at the beginning of the method, and any exceptions from that method will be captured and raised directly on that captured context. In the most common scenarios, this will cause an application-level exception, usually a crash. Some people call async void methods "fire-and-forget", but because of their exceptional behavior, I prefer "fire-and-crash". :)

"Avoid async void" is the general guideline, with one notable exception: event handlers (or items that are logically event handlers, such as ICommand.Execute implementations).

How does it work? How does this translate to aspnet?

It works just like any other async method. The main platform difference is that the UI thread doesn't need to know when the async method completes. ASP.NET needs to know that so it knows when to send the request, but the UI has no need to know when the async method completes. So async void works. It's still best avoided, because the calling code usually does need to know when it completes.

Sign up to request clarification or add additional context in comments.

Comments

6

Async void is only to be used for event handler/delegate comparability. that Execute is a event callback, likely from a DelegateCommand or similar.

The way it works is it treats it exactly the same as if you had a function that returned a Task but the caller never called await on that returned task.

On ASP.NET you are likely to never use async void and instead be using controllers that expose methods that return a Task<ActionResult>, use HostingEnviorment.QueueBackgroundWorkItem, or using functions that are wrapped up in a Page.RegisterAsyncTask in situations where you would have used async void in normal desktop programming.

public void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
}

2 Comments

What happens when I do not await the task?
@user2029276 the asp.net subsystem does not know about the task and may tear down the app domain for the website before the function has completed executing causing the work you told the function to do to be lost and never completed. The reason you can get away with this in WPF is your AppDomain won't get randomly torn down while the user is using the program, as long as the program is running the AppDomain lives on. A inactive user on a asp.net site could have a AppDomain torn down and not even notice it happen.

Your Answer

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.