I am building a Windows Service with .NET 4.0.
I have various unhandled exceptions thrown in Tasks, but they do not terminate my process as the MSDN documentation st
From Essential C# 4.0, page 715, the following might help you out:
The unhandled exception during the Task's execution will be suppressed until a call to one of the task completion members:
Wait(),Result,Task.WaitAll(), orTask.WaitAny(). Each ot these members will throw any unhandled exceptions that occurred within the task's execution.
Quite a few ways of handling exceptions are available. Have a look at MSDN here.
In answer to your comment, another quote from the same book explains why some exceptions are not propagated:
Although relatively rare, one of the exceptions for the general rule (of bubbling up) happens to be on Task. [..] Any Task-based exceptions thrown from the finalization queue during application exit will go suppressed. The behavior is set this way because frequently the effor to handle such an exception it too complex [...]
To overcome this, one way to do this elegantly is to create an exception handler task and to use ContinueWith to follow up after your task runs. You can then use parentTask.IsFaulted and gracefully crash, even in the event the exception is thrown in the finalization queue during application exit.
Tip: use the the flag OnlyOnFaulted to have this task run only when an exception occurs.