If you decide to offload some CPU intensive code from the UI thread by putting it in a task and running it in the Thread Pool, would this code itself ever benefit from using the “await” keyword?
It sort of seems like the answer is “no”..
I mean, one of the main reasons to await Tasks in the UI thread is to free the UI thread back up to do what it was doing before. If you await as Task while running on a Thread Pool, what are you freeing up that thread to do?
Am I missing something really obvious?
Just for a CPU-intensive task? Maybe not.
But that’s not the only reason to run code in thread pool thread.
Consider a web server – all the requests will be handled in thread pool threads… and by using
awaitwhile processing those requests (e.g. while waiting for a database or other web service response) you can handle millions of requests on very few threads.Additionally, even in a rich GUI scenario, you might want to put some long-running task onto a thread-pool thread, but still launch multiple asynchronous requests from that thread. The way that async/await encourages composition of asynchronous operations makes it entirely reasonable to write your code in a way which is asynchronous whichever thread it happens to run on.