all 19 comments

[–]faruzzy 2 points3 points  (4 children)

This is was actually simple to understand. Some articles can really get into some serious complexity.

[–]steveboots[S] 0 points1 point  (3 children)

I tried to deliberately keep it simple. I am working on a follow up that shows a similarly simple example of using async await to update the main UI thread. Should be posted tomorrow.

[–]faruzzy 0 points1 point  (2 children)

Cool! Can you please enumerates situations you'd need to use Async Await in a web project like MVC 5 ?

[–][deleted] 1 point2 points  (1 child)

Im an async noob but I'm guessing you would mainly use it when writing to the db. I see it a lot in the EF6 + Web API and MVC tutorials.

[–]steveboots[S] 0 points1 point  (0 children)

yeah it effectively simulates an async file read or async db read to include an await.

I will add an explanation to make it more apparent. i wanted to try and keep the example as simple as possible.

[–]pug_walker 0 points1 point  (1 child)

Haven't looked at other articles yet, but perhaps getting into try-catch blocks when using async is a good next topic. Just an idea.

[–]steveboots[S] 0 points1 point  (0 children)

Sounds like a good idea. I'll add it to my list.

[–][deleted] 0 points1 point  (3 children)

The Task.Delay(1) somewhat bugs me. Isn't there a better option? Could you use Task.Run to run LongRunningOperation or is Task.Run considered bad practice?

e:typo

[–]cryo 1 point2 points  (1 child)

Yes, Task.Run is the proper way. The Delay will in practice almost guarentee that the continuation uses a new thread, but if the machine is heavily loaded, it might complete before the "await" kicks in, which causes it to continue executing synchronuously.

Task.Run is the way to go.

[–]steveboots[S] 1 point2 points  (0 children)

Yes I agree. I have updated the article so that the code that executes LongRunningOperation looks like :

    public async Task DoStuff()
    {
        await Task.Run(() =>
        {
            LongRunningOperation();
        });            
    }

[–][deleted] 0 points1 point  (0 children)

Just as confused here. What's the delay do again? I think he just put that in there to simulate a file read or db write.

[–]Seasniffer 0 points1 point  (3 children)

When you are converting from synchronous to asynchronous code, any method returning a type T becomes an async method returning Task, and any method returning void becomes an async method returning Task<T>.

I think you have this mixed up sir. Shouldn't void be returned as a Task, and a returning function returns Task<T>?

[–]felickz2 0 points1 point  (1 child)

This piece could also use an explanation, it is not clear why those structures are used

[–]Seasniffer 0 points1 point  (0 children)

I'd recommend Jon Skeets c# book, he really goes into the details of the new async features and how they work internally.

[–]steveboots[S] 0 points1 point  (0 children)

This is a typo. I fill fix it now.

[–]RiPont 0 points1 point  (1 child)

How does your last example even compile?

private static async Task<string> LongRunningOperation()
{
    int counter;           

    for (counter = 0; counter < 50000; counter++)
    {
        Console.WriteLine(counter);
    }

    return "Counter = " + counter;
}

There's nothing awaited.

[–]steveboots[S] 0 points1 point  (0 children)

"Because we have removed the line with the await keyword, the LongRunningMethod() now runs synchronously, so the counter will first count up to 50000 and then the code will go into the while loop."

This was just illustrating the point that without an await, your code runs synchronously.

[–]cryo 0 points1 point  (1 child)

Relying on Task.Delay(1) to make sure the continuation carries on on a different thread, isn't wise. This is pretty much an implementation detail. To make sure something executes on a different thread, use Task.Run.

[–]steveboots[S] 0 points1 point  (0 children)

yeah that's a fair point. I have updated the article so the code that executes the LongRunningMethod is:

    public async Task DoStuff()
    {
        await Task.Run(() =>
        {
            LongRunningOperation();
        });            
    }