VS2010在64位版本的Windows上的WinForms应用程序中不显示未处理的异常消息 [英] VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows

查看:172
本文介绍了VS2010在64位版本的Windows上的WinForms应用程序中不显示未处理的异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个新项目时,我会收到一个奇怪的行为,用于未处理的异常。这是我如何重现的问题:

When I create a new project, I get a strange behavior for unhandled exceptions. This is how I can reproduce the problem:

1)创建一个新的Windows窗体应用程序(C#,.NET Framework 4,VS2010)

1) create a new Windows Forms Application (C#, .NET Framework 4, VS2010)

2)将以下代码添加到 Form1_Load 处理程序:

2) add the following code to the Form1_Load handler:

int vara = 5, varb = 0;
int varc = vara / varb;
int vard = 7;

我希望VS在第二行显示一个未处理的异常消息。但是,发生的情况是,第三行刚刚跳过,没有任何消息,应用程序继续运行。

I would expect that VS breaks and shows an unhandled exception message at the second line. However, what happens is that the third line is just skipped without any message and the application keeps running.

我现有的C#项目没有这个问题。所以我想我的新项目是用一些奇怪的默认设置创建的。

I don't have this problem with my existing C# projects. So I guess that my new projects are created with some strange default settings.

有没有人想到我的项目有什么问题?

Does anyone have an idea what's wrong with my project???

我尝试检查Debug-> Exceptions中的框。但是即使处理 try-catch 块中的异常,即使执行处理也会中断。这也不是我想要的。如果我记得正确,在这个对话框中有一个名为未处理的异常或类似的列,这样就可以做到这一点。但是在我的项目中只有一列(Thrown)。

I tried checking the boxes in Debug->Exceptions. But then executions breaks even if I handle the exception in a try-catch block; which is also not what I want. If I remember correctly, there was a column called "unhandled exceptions" or something like this in this dialog box, which would do excatly what I want. But in my projects there is only one column ("Thrown").

推荐答案

这是由wow64仿真引起的讨厌的问题允许32位代码在64位版本的Windows 7上运行。它会在运行的代码中响应64位窗口管理器生成的通知,例如 Load 事件。防止调试器看到它​​并进入。这个问题很难解决,微软的Windows和DevDiv小组来回指向手指。 DevDiv不能做任何事情,Windows认为这是正确和记录的行为,神秘的声音。

This is a nasty problem induced by the wow64 emulation layer that allows 32-bit code to run on the 64-bit version of Windows 7. It swallows exceptions in the code that runs in response to a notification generated by the 64-bit window manager, like the Load event. Preventing the debugger from seeing it and stepping in. This problem is hard to fix, the Windows and DevDiv groups at Microsoft are pointing fingers back and forth. DevDiv can't do anything about it, Windows thinks it is the correct and documented behavior, mysterious as that sounds.

它肯定是记录下来,但只是没有人理解后果或认为是合理的行为。特别是当窗口过程从视图中隐藏起来时,就像在任何使用包装类隐藏窗口管道的项目中。像任何Winforms,WPF或MFC应用程序。基础问题是Microsoft无法弄清楚如何将异常从32位代码流回64位代码,从而触发通知回到尝试处理或调试异常的32位代码。

It is certainly documented but just about nobody understands the consequences or thinks it is reasonable behavior. Especially not when the window procedure is hidden from view of course, like it is in any project that uses wrapper classes to hide the window plumbing. Like any Winforms, WPF or MFC app. Underlying issue is Microsoft could not figure out how to flow exceptions from 32-bit code back to the 64-bit code that triggered the notification back to 32-bit code that tries to handle or debug the exception.

只是附加调试器的问题,你的代码将像往常一样没有一个。

It is only a problem with a debugger attached, your code will bomb as usual without one.

项目>属性>构建选项卡>平台目标= AnyCPU和untick偏好32位。您的应用程序现在将作为64位进程运行,从而消除了wow64故障模式。一些后果,它会在VS2013之前禁用VS版本的Edit + Continue,并且当您对32位代码依赖时可能并不总是。

Project > Properties > Build tab > Platform target = AnyCPU and untick Prefer 32-bit. Your app will now run as a 64-bit process, eliminating the wow64 failure mode. Some consequences, it disables Edit + Continue for VS versions prior to VS2013 and might not always be possible when you have a dependency on 32-bit code.

其他可能的解决方法:

Other possible workarounds:


  • 调试>异常>勾选抛出框以使CLR异常强制调试器停止在引发异常的代码行。 / li>
  • 中加载事件处理程序中的try / catch,并在catch块中执行failfast。

  • Main()方法中使用 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException),以便消息循环中的异常陷阱在调试模式下未禁用。这使得所有未处理的异常都难以调试, ThreadException 事件是无用的。

  • 考虑如果你的代码真的属于加载事件处理程序。这是非常罕见的需要它,但它是非常受欢迎的VB.NET和天鹅歌曲,因为它是默认事件,双击轻松添加事件处理程序。当您对用户偏好和自动缩放应用后的实际窗口大小感兴趣时,您只需要需要加载

  • 更新到Windows 8或更高版本,他们已经解决了这个wow64问题。

  • Debug > Exceptions > tick the Thrown box for CLR exceptions to force the debugger to stop at the line of code that throws the exception.
  • Write try/catch in the Load event handler and failfast in the catch block.
  • Use Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) in the Main() method so that the exception trap in the message loop isn't disabled in debug mode. This however makes all unhandled exceptions hard to debug, the ThreadException event is pretty useless.
  • Consider if your code really belongs in the Load event handler. It is very rare to need it, it is however very popular in VB.NET and a swan song because it is the default event and a double-click trivially adds the event handler. You only ever really need Load when you are interested in the actual window size after user preferences and autoscaling is applied. Everything else belongs in the constructor.
  • Update to Windows 8 or later, they have this wow64 problem solved.

这篇关于VS2010在64位版本的Windows上的WinForms应用程序中不显示未处理的异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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