简体   繁体   中英

Eliding async and await flow

In the article about eliding async await , there is an example as follows:

public Task<string> GetElidingKeywordsAsync(string url)
{
    using (var client = new HttpClient())
        return client.GetStringAsync(url);
}

And he described the flow as follows:

With GetElidingKeywordsAsync, the code does this:

  1. Create the HttpClient object.

  2. Invoke GetStringAsync, which returns an incomplete task.

  3. Disposes the HttpClient object.

  4. Returns the task that was returned from GetStringAsync.

Why isn't the flow as follows?

  1. Create the HttpClient object.

  2. Disposes the HttpClient object.

  3. Invokes the GetStringAsync, and returns the task that was returned from GetStringAsync.

A using block without curly braces or semi-colon has an implied body:

public Task<string> GetElidingKeywordsAsync(string url)
{
    using (var client = new HttpClient())
        return client.GetStringAsync(url); // using body
}

This can be normalized to:

public Task<string> GetElidingKeywordsAsync(string url)
{
    using (var client = new HttpClient())
    {
        return client.GetStringAsync(url);
    }
}

Or written more compactly in C#8.0:

public Task<string> GetElidingKeywordsAsync(string url)
{
    using var client = new HttpClient();
    return client.GetStringAsync(url);
}

If you add a semi-colon, there would be an empty body, yielding the behavior you describe in OP:

public Task<string> GetElidingKeywordsAsync(string url)
{
    HttpClient client;
    using (client = new HttpClient());  // gets disposed before next statement
        return client.GetStringAsync(url);  // don't be fooled by the indent
}

This can be normalized to:

public Task<string> GetElidingKeywordsAsync(string url)
{
    HttpClient client;
    using (client = new HttpClient())
    {
    }
    return client.GetStringAsync(url);
}

Expanding on the comment given above, it would help if you see how the GetStringAsync method works.

Assuming the following to be the aysnc method:

public async Task<string> GetMethodAsync(string url)
{
    // perform some operations

    // execution will pause here due to await statement 
    // after calling download URL and the Task will be returned
    var result = await DownloadURL(string);

    //perform some more function
    // finally return the result
    return result;
}

Note here the method call to DownloadURL will be executed and then only the execution will pause to wait for results to come back, and a task is returned. Making a method Async does not in itself give you deferred execution, only the part after the await call gets "deferred".

That is the reason you will get an incomplete task and then in next step the client gets disposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM