0

Before you go and close this question answer provided here Parallel HTTP requests using System.Net.Http.HttpCliente does NOT answer my question at all.

I have the following scenario I want to execute the following RestApi calls in parallel

  • GetCountries (I want to wait for result to come back)
  • GetSomeOtherData (I dont need to wait but I want to preload in memory for use in next page)
  • Get SomethingElse (I dont need to wait but I want to preload in memory for use in next page)

My understanding of "WhenAll" is that it will wait for all to complete and that is not what I need to do.

Using "Task.Run" seems not appropriate as its not "CPU bound" stuff.

So in my head it says "Run them on a different thread" but isnt that what Task.Run does.

How do you handle a situation when you want to execute 3 rest api calls (httpClient) in parallel and you want wait for one and not for the others, the other's result by the time the user goes to the next page , they will be in memory as I will cache them for 1 minute.

EDITED

Is there a better way than below , Why did I do this: so that GetSomeOtherData and SomethingElse run at the same time as GetCountries,and they are not waiting/blocking for any to finish. Somehow it feels wrong but dont know how to improve on this.

    _ = Task.Run(async () => { await GetSomeOtherData (); })
    _ = Task.Run(async () => { await SomethingElse  (); })        
    var countries=await GetCountries();
5
  • await GetCountries(); await Task.WhenAll(GetSomeOtherData, SomethingElse); Commented Jul 24, 2022 at 9:34
  • @ErmiyaEskandary await Task.WhenAll(GetSomeOtherData, SomethingElse) means that both method have to finish before I can move on . Correct? what about running them all at the same time in parallel? The only thing I can think of see my edited answer Commented Jul 24, 2022 at 11:21
  • await Task.WhenAll(GetCountries, GetSomeOtherData, SomethingElse); is all in parallel but that's not what you mention in your post by 'wait for one and not for the others' Commented Jul 24, 2022 at 11:31
  • @ErmiyaEskandary reply appreciate your time in replying... and may be my post is not clear. GetCountries - I need to wait as It's used immediately the other 2 calls needs to be called but dont have to block anything and I will cache the results.... what about my edited solution, works but does not seem right - Commented Jul 24, 2022 at 11:40
  • "Task.Run" seems not appropriate as its not "CPU bound" CPU bound or not isn't relevant. The point is you don't want to wait for it to finish, so you need another thread. Your solution looks fine to me. Commented Jul 24, 2022 at 19:04

0

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.