2

I have the following Asynchronous method inside my AsyncController:

public async Task<Dashboard> GetFeeds()
{
    var movies = new HttpClient().GetStringAsync("http://netflix/api/MyMovies");
    var tweets = new HttpClient().GetStringAsync("http://twitter/api/MyTweets");

    await Task.WhenAll(movies, tweets);

    Dashboard dash = new Dashboard();
    dash.Movies = Deserialize<Movies >(movies.Result);
    dash.Tweets = Deserialize<Tweets >(tweets.Result);

    return dash; 
}

In this method do different call APIs, one with different return time from each other. What I can not understand about Task<> is because I have to wait for the return of the two to update my client? Being that I'm creating new threads.

enter image description here

Imagining that I play the return of each API in a PartialView, the result I thought would get:

-First I would have my Movies list (it only takes 5s), -> Show for my user

-And Then would my list of Tweets -> Show for my user

But what I see is:

-While The Twitter request does not end I did not get to play the data I got from Netflix on-screen for my user.

The big question is: A Task<> serves only for the processing to be done faster?

I can not play the information on the screen according to the turnaround time of each API that I ordered?

This is the call to my method

public async Task<ActionResult> Index()
{
    var feeds = await GetFeeds();

    return View(feeds);
}

I confess, I'm very confused, or, maybe you did not understand the concept of Task<>.

1
  • 1
    You'll want to create two different async operations. One that loads and displays the Twitter information (aka the longer running operation) and one that displays the Netflix information (aka the shorter operation). await Task.WhenAll(movies, tweets); is waiting on both of them to complete before it does anything with displaying them. I think you need to do more research on TPL with async/await. I sadly don't have good resources on it. You might look up the pattern on MSDN's Channel 9. They're usually really good. Commented Dec 5, 2014 at 16:51

1 Answer 1

8

The way ASP.NET MVC works is that a single controller action handles a single HTTP request, and produces a single HTTP response. This is true whether the action is synchronous or asynchronous.

In other words (as I explain on my blog), async doesn't change the HTTP protocol. To return an "initial result" to the client browser and update the page (or part of the page) with other data, you'll need to use a technology designed for that: AJAX, or SignalR.

For more information, see the "Asynchronous Code Is Not a Silver Bullet" section of my MSDN article on async ASP.NET.

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

4 Comments

Yes, thanks for the clarification, I already have it using AJAX request. I just thought I would return to the user so that each request over. I'll read your material. Thank you!
So to say that a Task<> is just a faster way to make a request, it started a new thread?
@Igao: No. Task enables asynchronous processing. It's not necessarily faster, and does not create new threads. I have an async intro blog post you may find helpful.
@Igao The use of the TPL when handling ASP requests is almost entirely so that the thread pool thread handling the request is able to spend time working on some other request whenever you're performing long running IO bound operations (usually database or network calls). It prevents the thread pool thread from needing to sit there and do nothing while waiting on the IO. Since there is a limit on how many thread pool threads the machine can support, this means increased throughput when it comes to handling requests. Each individual request at about the same speed.

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.