Why doesn't my process terminate when Task has unhandled exception?

后端 未结 5 731
别那么骄傲
别那么骄傲 2020-12-14 20:11

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

5条回答
  •  误落风尘
    2020-12-14 20:43

    You may create it with TaskCreationOptions.AttachedToParent. According to Nested Tasks and Child Tasks (MSDN), Exceptions are then propagated to your thread. I do not know, however, whether this is elegant or not.

    Microsoft does not recommend this "in most cases". Somebody else might know in what case this may be sensible. From the same article:

    You can use attached child tasks to create tightly-synchronized graphs of asynchronous operations. However, in most scenarios, we recommend that you use nested tasks because the relationships with other tasks are less complex. That is why tasks created inside other tasks are nested by default, and you must explicitly specify the AttachedToParent option to create a child task.

    Cheers, Matthias

提交回复
热议问题