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

后端 未结 5 690
别那么骄傲
别那么骄傲 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:40

    According to this nice blog post if you want to crash your app as soon as you have unhandled exception in your task then you can continue your task something like below:

    public static Task FailFastOnException(this Task task) 
    { 
        task.ContinueWith(c => Environment.FailFast(“Task faulted”, c.Exception), 
        TaskContinuationOptions.OnlyOnFaulted | 
        TaskContinuationOptions.ExecuteSynchronously | 
        TaskContinuationOptions.DetachedFromParent); 
        return task; 
    }
    

提交回复
热议问题