I am using a task based (TPL) threading model. When starting my application (WPF) I register TaskScheduler.UnobservedTaskException to display a Message.
However if an exception is raised (e.g.Task.Factory.StartNew(() => throw new Exception());) it takes quite a significant amount of time (3-10 seconds) before the UnobservedTaskException event is raised.
I want it to be raised immediatly (or at least very quick), any idea on what could be the case here?
Regards
This is because the finalizer for that thread has not run yet. Since this occurs with a Garbage Collection, this non-deterministic. Additionally, how does the CLR determine whether to throw it immediately or you wanted to check the task’s result or call
Wait()and observe the exception yourself?If you enable Break on First Chance Exceptions, it will get thrown while debugging.
It will be propagated back to the joining thread, guaranteed. If you are using .NET 4.0, your process will terminate. If you are using .NET 4.5, it won’t