In my controller, I need to call the BlockingHttpRequest() method, which makes an HTTP request. It is relatively slow, and blocks the flow.
I cannot reorganize this method to make it asynchronous.
Is it better to wrap this method in Task.Run to at least free the UI / controller thread?
I'm not sure if it really improves anything or just does more work.
public class MyController : Controller { public async Task<PartialViewResult> Sync() { BlockingHttpRequest(); return PartialView(); } public async Task<PartialViewResult> Async() { await Task.Run(() => BlockingHttpRequest()); return PartialView(); } }
In practice, I have a couple of similar situations where BlockingHttpRequest() takes 500 ms and 5000 ms, respectively.
I understand that Task.Run() will not return my function before.
I thought there might be some benefit to increasing bandwidth. Having freed up the controller thread, does it make it accessible to other users? This would mean that in MVC there is a difference between controller threads and workflow threads.
source share