Finally Block Not Running?

后端 未结 5 1690
说谎
说谎 2020-12-09 09:56

Ok this is kind of a weird issue and I am hoping someone can shed some light. I have the following code:

static void Main(string[] args)
{
    try
    {
             


        
5条回答
  •  一生所求
    2020-12-09 09:59

    I am able to reproduce the behavior now (I didn't get the exact steps from your question when I was reading it the first time).

    One difference I can observe is in the way that the .NET runtime handles the unhandled exception. The CLR 2.0 runs a helper called Microsoft .NET Error Reporting Shim (dw20.exe) whereas the CLR 4.0 starts Windows Error Reporting (WerFault.exe).

    I assume that the two have different behavior with respect to terminating the crashing process. WerFault.exe obviously kills the .NET process immediately whereas the .NET Error Reporting Shim somehow closes the application so that the finally block still is executed.

    Also have a look at the Event Viewer: WerFault logs an application error notifying that the crashed process was terminated:

    Application: ConsoleApplication1.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.Threading.AbandonedMutexException
    Stack:
       at Program.Main(System.String[])
    

    dw20.exe however only logs an information item with event id 1001 to the Event Log and does not terminate the process.

提交回复
热议问题