0

I have a web page with multiple tabs. One tab contains a lot of data, and for this reason I need to write an async method, because in synchronous way it takes long loading the tab.

I tried to do, but it seems that it behaves synchronous.

This is my controller method:

public async Task<ActionResult> ProductDetails(int id, string selected, string category)
{
 ..........
 ///Code
  #region Matching Vehicles tab               

  List<MatchingVehiclesVM> matchingVehicles = new List<MatchingVehiclesVM>();
  productVM.ProductMatchingVehicles = await GetMatchingVehiclesAsync(product.Id);
  #endregion 
}

public async Task<List<MatchingVehiclesVM>> GetMatchingVehiclesAsync(int id)
{
    return await Task.Run(() => GetMatchingVehicles(id));
} 

private List<MatchingVehiclesVM> GetMatchingVehicles(int id)
{
  //contains the code that returns from database the needed list
}

Can you advise what I had missed here to make it to work in async way? Thanks!

8
  • but it seems that it behaves synchronous. why do you think so? Commented Mar 8, 2017 at 11:07
  • I don't think you quite understand what asynchronous means in this context. Are you expecting one of the tabs on your page to populate after the page has been sent to the client? Because, that's not what will happen. Commented Mar 8, 2017 at 11:10
  • @DavidG - Yes, this is the way it should work. Commented Mar 8, 2017 at 11:11
  • @Orsi, asynchronous means that your thread won't be blocked until you get data. The load speed will not be increased. Commented Mar 8, 2017 at 11:13
  • 1
    Well that's a very different question. You really need to go and learn about that yourself. Start by Googling "AJAX" and you should be fine. Commented Mar 8, 2017 at 11:19

1 Answer 1

1

Few important points to note, changing controller to async will not reduce the operation time. Async request will take almost same time as sync request. In async flow, when request come to web server, a worker thread is taken from thread pool to process. During async operation, worker thread will be return to thread pool to process other web request. When async operation completes, thread is taken again from thread pool and request is further processed. This will improve scale of your web application, however individual request will take almost same time.

From client perspective call is still synchronous. You may use ajax to make async call from client and load other tab in background. Ajax call from client will not block your web page.

Very basic ajax example

    $.ajax({url: "products/productId1/details", success: function(result){
        $("#productDescription").html(result);
    }});

For more details information please refer jquery ajax - http://api.jquery.com/jquery.ajax/

Async controller mvc - https://learn.microsoft.com/en-us/aspnet/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4

Hope this helps.

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

Comments

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.