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

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

问题描述

好吧,这是一个奇怪的问题,我希望有人能够轻视。我有以下代码:

  static void Main(string [] args)
{
try
{
Console.WriteLine(in try);
throw new EncoderFallbackException();
}
catch(异常)
{
Console.WriteLine(in Catch);
抛出新的AbandonedMutexException();
}
finally
{
Console.WriteLine(in Finally);
Console.ReadLine();
}
}

当我编译为目标3.5(2.0 CLR )会弹出一个XXX已经停止工作的窗口。如果我现在点击取消按钮,它将运行最后一个AND,如果我等待它完成查找并点击关闭程序按钮,它也将运行最后。



现在有趣和令人困惑的是,如果我做同样的事情编译为4.0点击取消按钮将运行finally块并点击在关闭程序按钮不会。我的问题是:为什么最终在2.0上运行,而在打开关闭程序按钮时不能在4.0上运行?这是什么影响?



编辑:我正在Windows 7 32位的发行模式(内置在发行模式)的命令提示符下运行。错误消息:下面的第一个结果是运行在3.5点击关闭后窗口查找问题,其次是当我在4.0上运行它做同样的事情。



解决方案

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



我可以观察到一个区别是.NET运行时处理未处理的异常的方式。 CLR 2.0运行一个名为Microsoft .NET错误报告消息( dw20.exe )的帮助器,而CLR 4.0启动Windows错误报告( WerFault.exe )。



我假设两者在终止崩溃过程WerFault.exe显然会立即杀死.NET进程,而.NET错误报告器则以某种方式关闭应用程序,以使最终的程序块仍然被执行。



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

 
应用程序:ConsoleApplication1.exe
框架版本:v4.0.30319
说明:由于未处理的异常,进程已终止。
异常信息:System.Threading.AbandonedMutexException
堆栈:
在Program.Main(System.String [])

dw20.exe,但只记录事件ID为1001的信息项到事件日志,并且不会终止进程。


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();
    }
}

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.

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.

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?

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

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.

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

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