最后阻止不运行? [英] Finally Block Not Running?

查看:207
本文介绍了最后阻止不运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是一个奇怪的问题,我希望有人能够散发一些光。我有以下代码:

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
    {
        Console.WriteLine("in try");
        throw new EncoderFallbackException();
    }
    catch (Exception)
    {
        Console.WriteLine("in Catch");
        throw new AbandonedMutexException();
    }
    finally
    {
        Console.WriteLine("in Finally");
        Console.ReadLine();
    }
}



现在我编译到目标3.5 )会弹出一个窗口,说XXX已停止工作。如果我现在点击取消按钮,它将运行最后,如果我等待,直到它完成查找并单击关闭程序按钮,它也将运行finally中。

NOW when I compile this to target 3.5(2.0 CLR) it will pop up a window saying "XXX has stopped working". If I now click on the Cancel button it will run the finally, AND if I wait until it is done looking and click on the Close Program button it will also run the finally.

现在有趣的和令人困惑的是,如果我做同样的事情编译对4.0点击取消按钮将运行finally块,在关闭程序按钮不会。

Now what is interesting and confusing is IF I do the same thing compiled against 4.0 Clicking on the Cancel button will run the finally block and clicking on the Close Program button will not.

我的问题是:为什么终于运行在2.0而不是在4.0打开关闭程序按钮?这是什么影响?

My question is: Why does the finally run on 2.0 and not on 4.0 when hitting the Close Program button? What are the repercussions of this?

编辑:我在Windows 7 32位的发布模式(内置版本模式)的命令提示符下运行此命令。错误消息:下面的第一个结果运行在3.5打开关闭窗口查找问题,第二个是我在4.0上运行它,并做同样的事情。

I am running this from a command prompt in release mode(built in release mode) on windows 7 32 bit. Error Message: First Result below is running on 3.5 hitting close after windows looks for issue, second is when I run it on 4.0 and do the same thing.

推荐答案

我可以重现现在的行为(当我第一次阅读它时,我没有得到你的问题的确切步骤)。

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).

是.NET运行时处理未处理的异常的方式。 CLR 2.0运行一个名为 Microsoft .NET错误报告Shim dw20.exe )的帮助程序,而CLR 4.0启动Windows错误报告( WerFault.exe )。

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).

我假设两者在终止崩溃过程。 WerFault.exe显然立即杀死.NET进程,而.NET错误报告Shim一定会关闭应用程序,以便finally块仍然被执行。

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.

还有一个看看事件查看器:WerFault记录了一个应用程序错误,通知崩溃的进程已终止:

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只会将事件ID为1001的信息项记录到事件日志中,并且不会终止进程。

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

这篇关于最后阻止不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆