I highly recommend you use Task.Run over ThreadPool.QueueUserWorkItem .
Take a step back first. What is the purpose of DoSomethingCpuIntensive ? If you are calculating some value, I suggest you return it, so instead of Task you have Task<T> . for example (assuming the DoMoreStuff not CPU intensive):
async Task DoWorkAsync() { var obj = await Task.Run(() => DoSomethingCpuIntensive()); obj.DoMoreStuff(); }
The idea I'm trying to understand is that your code should take care of the results of your background operations, even if the “result” is simply “success” or “exception”. Your code is cleaner. Otherwise, you have a semi-independent system that you can respond to by detecting changes in the state of your application. Much more randomly.
However, if you really want to have a semi-independent system, and you want it to break your process, if it fails, then ThrowUnobservedTaskExceptions is exactly what you want. I'm not sure why you think this will not help.
source share