0

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.

Manish
  • 37
  • 6

2 Answers2

0

Make an dictionary/JSON of responses and share this variable with the Tasks, so when any finish will fill it's part.

And you can make a refresh period to add the new results using AJAX while some task is still running.

Maybe this give you some ideas how to refresh Refresh/reload the content in Div using jquery/ajax

Community
  • 1
  • 1
Raskayu
  • 735
  • 7
  • 20
0

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
Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28