Manish Anand Manish Anand - 1 year ago 69
Ajax Question

Populating results coming from different APIs running in different threads in View

I am working in ASP.Net 4.0 MVC project, where i am calling different APIs in Different threads at a same time. I am using Task.Factory.StartNew() for this purpose which has event .ContinueWith(), will run on completion of each threads.

My Query is how will i populate data coming at different times (as APIs are taking different time), in View (Guess it has some ajax involved). Currently I am waiting for all the thread to complete and then at last populating all the result at the end.

Need some Guidance to proceed to save some time.

Answer Source

You can't update the view after your action has returned by using the MVC framework only. What you can do

  1. Mark your action as async and await inside all tasks(await Task.WhenAll) - this will save you one thread(the one which handles the request and you're currently blocking) but the user experience will be the same - he'll have to wait for all to see the complete view
  2. You can return the view immediately, without starting any API calls. The view should consist of multiple parts, which will, using AJAX call other actions to process each API call. This will work only if the view is composed of different regions that depend on the result on a single API. If you need a lot of API's data for a single field on the view I would go with approach #1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download