Ames ISU
Ames ISU

Reputation: 407

Async and await in a C# example

I found the following example from Microsoft website about Async and await ( https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/):

// Three things to note in the signature:  
//  - The method has an async modifier.   
//  - The return type is Task or Task<T>. (See "Return Types" section.)  
//    Here, it is Task<int> because the return statement returns an integer.  
//  - The method name ends in "Async."  
async Task<int> AccessTheWebAsync()  
{   
    // You need to add a reference to System.Net.Http to declare client.  
    HttpClient client = new HttpClient();  

    // GetStringAsync returns a Task<string>. That means that when you await the  
    // task you'll get a string (urlContents).  
    Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");  

    // You can do work here that doesn't rely on the string from GetStringAsync.  
    DoIndependentWork();  

    // The await operator suspends AccessTheWebAsync.  
    //  - AccessTheWebAsync can't continue until getStringTask is complete.  
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync.  
    //  - Control resumes here when getStringTask is complete.   
    //  - The await operator then retrieves the string result from getStringTask.  
    string urlContents = await getStringTask;  

    // The return statement specifies an integer result.  
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value.  
    return urlContents.Length;  
}  

My first question is from the webpage "If AccessTheWebAsync doesn't have any work that it can do between calling GetStringAsync and awaiting its completion, you can simplify your code by calling and awaiting in the following single statement." It seems to imply that the following statement is really doing something:

    Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");  

Is that true? I though it's just a definition of a task, but it has not yet started/run.

Second question is I tried to change the implementation as follows to check my idea:

async Task<int> AccessTheWebAsync()  
{    
    HttpClient client = new HttpClient();  

    //Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");
        Task<string> getStringTask = DoPriorWork();  

    // You can do work here that doesn't rely on the string from GetStringAsync.  
    DoIndependentWork();  

    string urlContents = await getStringTask;  

    return urlContents.Length;  
}  
void DoIndependentWork()
    {
        resultsTextBox.Text += "Working . . . . . . .\r\n";
    }

    Task<string> DoPriorWork()
    {
        return new Task<string>(
            () =>
            {
                string retStr = "good";
                Debug.Assert(true, "running dopriorwork");
                System.Threading.Thread.Sleep(10000);
                return retStr;
            }
        );
    }

However, the method DoPriorWork() didn't execute at all. What is wrong with this implementation? Thanks!

Upvotes: 4

Views: 373

Answers (1)

Karel Tamayo
Karel Tamayo

Reputation: 3760

Is that true? I thought it's just a definition of a task, but it has not yet started/run.

Indeed. The operation you called returned immediately, the task you're given is "hot" - i.e already running. That's why you get the task actually, so you can await it at a later point. Thus you get a way to run other stuff, as the example states.

Second question is I tried to change the implementation as follows to check my idea...

To be able to get the code running, try changing the the method DoPriorWork as follows:

private Task<string> DoPriorWork()
{
    return Task.Run(() =>
    {
        var retStr = "good";
        Console.WriteLine("running dopriorwork");
        Thread.Sleep(10000);
        return retStr;
    });
}

Note that instead of returning new Task instance, which would provide me with a Task in Created (not running) state, I'm queueing and returning a "hot" task, using the static method Task.Run().

Hope this helps!

Upvotes: 1

Related Questions