Is it a bad idea to do this?
var result = await Task.Run(CPUBoundWork);
NotAwaitedCPUWork();
Is that because the work that follows the await
statement, i.e. the continuation, which in this case would be the method call to NotAwaitedCPUWork
would simply contest for the CPU with the awaited work represented by the task that has already been scheduled, and this would unnecessarily put load on the task scheduler to inject more threads or queue up the work on an existing thread; as a result, the continuation will anyway have to wait until a thread becomes available?
If this explanation is the most plausible one, then what is the correct way to run CPU bound work with a task assuming that you do care about the result of the CPU-bound work? Do you just block the current thread by Task.Wait()
ing on the CPU-bound work?
What is the recommended practice for awaiting the completion of CPU-bound work when you care about the result of the work?