DoubleVoid
DoubleVoid

Reputation: 795

async await blocking, what is wrong?

I am trying to test the async/await feature of .NET. I setup a little test in LinqPad:

void Main(string[] args)
{
    Test.DoStuff().Wait();
}

// Define other methods and classes here
public class Test
{
    public async static Task DoStuff()
    {
        string s1 = "Hello 1";
        string s2 = "Hello 2";
        var s3 = await Test.Gimme();
        string s4 = "Hello 4";

        Console.WriteLine(s1);
        Console.WriteLine(s2);
        Console.WriteLine(s3);
        Console.WriteLine(s4);
    }

    public async static Task<string> Gimme()
    {
        return await Task.Run<string>(() => RetString());
    }

    public static string RetString()
    {
        Console.WriteLine("Begin");

        for (int i = 0; i < 90000000; i++)
        {
            string s = "FOO";
            s = s + s;
            s = s + s;
        }

        Console.WriteLine("End");
        return "Task Hello 3!";
    }
}

From what I understood, I thought the code will get executed until s3 gets used again => Console.WriteLine(s3);

What am I missing?! I thought the await keyword makes it so, that the program keeps doing it's stuff, as long as the awaited result is not used.

Maybe I am implementing the Task wrong?!

Upvotes: 0

Views: 389

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 659964

What am I missing?! I thought the await keyword makes it so, that the program keeps doing it's stuff, as long as the awaited result is not used.

You are correct; your confusion is what "doing its stuff" means. The "stuff" that is done while you are awaiting the result is the "stuff" of the caller of DoStuff, which is Main. What does the caller do while DoStuff cannot make progress because the task is not completed? It calls Wait. So that's what happens!

You said that you wanted to await the result, meaning that you want the caller to do whatever it does while the task is being processed asynchronously. And the caller says "I have nothing to do but wait synchronously*. If the caller did some other work before calling Wait on the task, then you would see that work being done.

Upvotes: 5

David
David

Reputation: 218798

I thought the await keyword makes it so, that the program keeps doing it's stuff, as long as the awaited result is not used.

You are mistaken. The await keyword will await that operation. That is, instead of returning a Task which may complete the operation at a later time, the keyword awaits the result of that Task. (Which in this case is a string.)

So when you do this:

var s3 = await Test.Gimme();
string s4 = "Hello 4";

That second line doesn't execute until the result of the async operation is available. As opposed to this:

var s3 = Test.Gimme();
string s4 = "Hello 4";

Which doesn't wait for the operation to complete and instead just puts a reference to the Task<string> in s3, which may complete (be awaited) at a later time.

The idea here is that the DoStuff() method itself will execute code in the order in which it is imperatively written, just like any other code. But something invoked by that code is potentially asynchronous, and so code which in turn invokes DoStuff() has the option of handling it in an asynchronous manner.

Most async methods are logically synchronous, but wrap underlying asynchronous operations and expose the option to higher levels to not block on those operations. Keeping the mantra that things should be "async all the way down", many such methods offer "pass-through ascynchronicity" and allow the top-level UI to handle bottom-level I/O asynchronously.

Upvotes: 7

striker42382
striker42382

Reputation: 21

Await actually holds execution until the process if fully complete. So, your test method should fully build out the whole RetString, then return that to Gimme, then Gimme will return the string to var s3, then processing continues.

From Microsoft: The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work. Further details found here: https://msdn.microsoft.com/en-us/library/hh156528.aspx

Upvotes: 2

Related Questions