在Try / Catch里面,Visual Studio在Unhandled异常中断和终止应用程序 [英] Inside Try/Catch, Visual Studio breaks and ends app on Unhandled exception

查看:215
本文介绍了在Try / Catch里面,Visual Studio在Unhandled异常中断和终止应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Private Sub MyApplication_Startup(...)处理Me.Startup 
'---未处理的异常处理程序
AddHandler AppDomain.CurrentDomain.UnhandledException,AddressOf UnhandledExHandler
'---处理程序0
AddHandler System.Windows.Forms.Application.ThreadException,AddressOf ThreadExHandler
Utils.RememberMainThreadId()
End Sub

Sub OpenMyForm )'入口点
Debug.Assert(Utils.RunningOnMainThread())
尝试
MyForm.Show()
Catch ex As Exception'---处理程序1
LogError(ex)'---只有在Visual Studio
之外启动时,才会在此处运行尝试
End Sub

Sub MyForm_Load()处理MyBase.Load
调试.Assert(Utils.RunningOnMainThread())
FillMyDataTable()'---如果我把try / c atch这里,它将始终工作(测试)
End Sub

Sub FillMyDataTable()
尝试
新的SqlClient.SqlDataAdapter(sqlCmd).Fill(myDataTable)
Catch ex As Exception'--- handler 2
如果ex.Number = Constants.ConnectionBroken然后
ReconnectRetry()
Else
抛出'---进入UnhandledExHandler( )当在Visual Studio
结束如果
结束尝试
结束Sub

在Visual Studio中,错误的SQL命令中的错误是
UnhandledExHandler()
但是如果在VS外部启动相同的EXE, code>在内抓取ex As Exception Wrapper()被触发(这是预期的结果)。这里有什么问题?




  • Visual Studio错误消息是类型为System.Data的未处理的异常.SQLClient.SqlException'发生在filename.exe


  • 这是在调用$ code MyForm时发生的。 Show()间接调用 MyForm_Load()事件处理程序,它调用 MyComboBox_SelectedIndexChanged()事件处理程序(并且这一个间接执行查询)。


  • 如果我找到并单击 Wrapper()堆栈框架在休息时异常,我可以看到在之间的 FillMyDataTable()的高亮调用尝试 / Catch


  • 我总是在主线程( ManagedThreadId 存储在启动事件中,然后多个断言验证是否插入上述代码中。


  • 堆栈跟踪完全相同(通过diff检查),除了最底层( System.Windows.Forms .NativeWindow.DebuggableCallback() vs. System.Windows.Forms.NativeWindow.Callback())。




    • 最上面的框架是 System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection)


  • 直到昨天,这不是问题。我想我已经改变了一些选项。


  • Visual Studio的重新启动没有帮助。


  • p>如果启用或禁用异常助理,行为没有变化。


  • 选项和异常设置如下所示:




  • 堆栈跟踪是:




 
myApp.exe!myApp.frmAPP_PrenosWizard.frmFT_PrenosWizard_Load(Object sender,System.EventArgs e)Line 125 Basic
System.Windows.Forms.dll!System.Windows.Forms.Form.OnLoad(System .EventArgs e)+ 0x1d5 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.OnCreateControl()+ 0x55 bytes
System.Windows.Forms.dll!System.Windows.Forms .Control.CreateControl(bool fIgnoreVisible)+ 0x181 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl()+ 0x24 bytes
System.Windows.Forms.dll!System .Windows.Forms.Control.WmShowWindow(ref System.Windows.Forms.Message m)+ 0x98 bytes
System.Windows.Forms.dll!System.Windows.F orms.Control.WndProc(ref System.Windows.Forms.Message m)+ 0x2b6 bytes
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m )+ 0x2a bytes
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m)+ 0x10 bytes
System.Windows.Forms.dll !System.Windows.Forms.Form.WmShowWindow(ref System.Windows.Forms.Message m)+ 0x41 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows .Forms.Message m)+ 0x154 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)+ 0x10 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)+ 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms .NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg,System.IntPtr wparam,System.IntPtr lpar am)+ 0x57 bytes
[本机到托管转换]
[管理到本地转换]
System.Windows.Forms.dll!System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(int dwExStyle, string lpszClassName,string lpszWindowName,int style,int x,int y,int width,int height,System.Runtime.InteropServices.HandleRef hWndParent,System.Runtime.InteropServices.HandleRef hMenu,System.Runtime.InteropServices.HandleRef hInst,object pvParam )+ 0x3c bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp)+ 0x225 bytes
System.Windows.Forms.dll! System.Windows.Forms.Control.CreateHandle()+ 0x125 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.CreateHandle()+ 0x9f bytes
System.Windows.Forms。 dll!System.Windows.Forms.Control.Handle.get()+ 0x45 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value)+ 0x160 bytes
System.Windows.Forms.dll中!System.Windo我的.clsEntita.enEntita cielEntita,整数cielID,System.Data.SqlClient.SqlConnection cn1)行8212 + 0xa字节基本
myApp.exe!myApp.frmMain.FCreateTransferUI(myApp.frmAPP_PrenosWizard.enRezimPrenosu模式,myApp.APP.clsEntita .enEntita sourceEntity,Integer sourceID,myApp.APP.clsEntita.enEntita targetEntity,Integer targetID,System.Data.SqlClient.SqlConnection cn1)行2410 + 0x17 bytes Basic
myApp.exe!myApp.frmMain.frmMain_Receive(Object sender ,myApp.clsFisCommandProcessor.ReceivedEventArgs e)第239行+ 0xa7字节基本
myApp.exe!myApp.clsFisCommandProcessor.raise_Received(对象发件人,myApp.clsFisCommandProcessor.ReceivedEventArgs e)第96行+ 0x2e字节基本
myApp。 exe!myApp.clsFisCommandProcessor.Execute(myApp.clsFisCommand命令,对象发件人)行136 + 0x57 bytes基本
myApp.exe!myApp.clsFisCommandProcessor.Execute(myApp.clsFisCommand()命令,对象发件人)行128 + 0x27字节基本
myApp.exe!myApp.clsFisCommandProcessor.Execute(String扫描,布尔requireMarking,对象发件人)行122 + 0x30字节基本
myApp.exe!myApp.frmCommandPad.TSMI_Execute_Click(对象发件人,System.EventArgs e)行94 + 0x51字节基本
System.Windows。 Forms.dll!System.Windows.Forms.ToolStripItem.RaiseEvent(object key,System.EventArgs e)+ 0x58 bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs e)+ 0x46 bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs e)+ 0x6e bytes
System.Windows.Forms.dll!System.Windows .Forms.ToolStripItem.FireEventInteractive(System.EventArgs e,System.Windows.Forms.ToolStripItemEventType met)+ 0x83 bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.FireEve nt(System.EventArgs e,System.Windows.Forms.ToolStripItemEventType met)+ 0x118 bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.ProcessCmdKey(ref System.Windows.Forms.Message m ,System.Windows.Forms.Keys keyData)+ 0x47 bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripManager.ProcessShortcut(ref System.Windows.Forms.Message m,System.Windows.Forms .Keys快捷方式)+ 0x2dc bytes
System.Windows.Forms.dll!System.Windows.Forms.ToolStripManager.ProcessCmdKey(ref System.Windows.Forms.Message m,System.Windows.Forms.Keys keyData)+ 0x2d字节
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)+ 0x3c bytes
系统.Windows.Forms.dll!System.Windows.Forms.Form.ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)+ 0x29 bytes
System.Windows.Forms.dll !System.Windows.Forms.Control.ProcessCm dKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)+ 0x96 bytes
System.Windows.Forms.dll!System.Windows.Forms.TextBoxBase.ProcessCmdKey(ref System.Windows Forms.Message msg,System.Windows.Forms.Keys keyData)+ 0xda bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessMessage(ref System.Windows.Forms.Message msg) + 0x90 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessControlMessageInternal(System.Windows.Forms.Control target,ref System.Windows.Forms.Message msg)+ 0x101 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg)+ 0xf6 bytes
System.Windows.Forms.dll!System。 Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg)+ 0x5 bytes
System.Windows.Forms.dll!System.Windows .Forms.A pplication.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID,int reason,int pvLoopData)+ 0x22e bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext。 RunMessageLoopInner(int reason,System.Windows.Forms.ApplicationContext context)+ 0x177 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason,System.Windows.Forms。 ApplicationContext上下文)+ 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context)+ 0x18 bytes
Microsoft.VisualBasic.dll! Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()+ 0x81 bytes
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()+ 0xef bytes
Microsoft.VisualBasic.dll!Microsoft。 VisualBasic.ApplicationServices.WindowsFormsApplica orderBase.Run(string [] commandLine)+ 0x68 bytes
[本机到托管转换]
[管理到本地转换]
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection。装配汇编,字符串[] args)+ 0x19字节
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel)+ 0x6e bytes
mscorlib.dll!System.Runtime.Hosting.ManifestRunner .ExecuteAsAssembly()+ 0x84 bytes
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext,string [] activationCustomData)+ 0x65 bytes
mscorlib.dll!System.Runtime。 Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext)+ 0xa bytes
mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext)+ 0x3e bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll! Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()+ 0x23 bytes
mscorli b.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x66字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state )+ 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()+ 0x44 bytes


解决方案

这个问题有很多动作部分,不知道我可以对所有人都公正。起点是,如果您使用调试器,则不应订阅这些事件。将代码用包装如果不是System.Diagnostics.Debugger.IsAttached然后,那么您可以正确地诊断和修复未处理的异常。



一个重要的随机数是在Load事件处理程序中抛出的异常会发生什么。 Microsoft在Windows回调活动时正确处理异常引发的大量麻烦。例如导致Load事件触发的那个。他们在Win8再次在Win8上改变了Vista的规则。如果您运行64位版本的32位,它们是不同的。您的程序是否以32位或64位进程运行。使用调试器改变行为。当出现异常时,程序兼容性助理可能出现,询问您是否希望程序兼容。大家都说是的,当然是个坏主意。添加它们,您可以获得三十二分之一的不同结果。 Ober。



正式描述在这个MSDN文章,但没有人理解。直到它不做他们希望的,包含在一个问题像这样一个。缓解情况是,当您调试应用程序时,这只有字节,消息循环中的正常异常后台可以确保在用户的机器上不会出现这种情况。换句话说,当您按Ctrl + F5或使用资源管理器运行程序时,您看到的是您可以期望的工作方式。



这是一个影响Windows上的任何 GUI程序。但是它在Winforms应用程序中的诀窍特别麻烦,程序员完全使用Load事件。由于它是Form类的默认事件,它需要双击才能生成它。特别是在VB.NET中,它不会自动为窗体创建构造函数,Visual Basic具有使用Load事件来初始化窗体的遗留,返回到VB6。


$ b $我通常避免判断我看到的编程实践,但是由于这些问题,使用Load事件今天是一个非常糟糕的做法。始终喜欢构造函数来初始化一个类,一个.NET程序员知道的规则,它与Form类没有什么不同。在这篇文章中涵盖 使用加载的原因很少。


I'm getting different behavior when running the same code inside and outside the Visual Studio.

Private Sub MyApplication_Startup(...) Handles Me.Startup
    '--- handler for unhandled exceptions
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExHandler
    '--- handler 0
    AddHandler System.Windows.Forms.Application.ThreadException, AddressOf ThreadExHandler
    Utils.RememberMainThreadId()
End Sub

Sub OpenMyForm()  'entry point
    Debug.Assert(Utils.RunningOnMainThread())
    Try
        MyForm.Show()  
    Catch ex As Exception  '--- handler 1
        LogError(ex)   '--- goes here only if launched outside the Visual Studio
    End Try
End Sub

Sub MyForm_Load() Handles MyBase.Load
    Debug.Assert(Utils.RunningOnMainThread())
    FillMyDataTable() '---if I put try/catch here, it will always work (tested)
End Sub

Sub FillMyDataTable()
    Try
        New SqlClient.SqlDataAdapter(sqlCmd).Fill(myDataTable)
    Catch ex As Exception '--- handler 2
        If ex.Number = Constants.ConnectionBroken then
            ReconnectRetry()
        Else
            Throw  '--- enters UnhandledExHandler() when in Visual Studio
        End If
    End Try
End Sub

In Visual Studio, error in bad SQL command goes to UnhandledExHandler(), but if the same EXE is launched outside the VS, Catch ex As Exception inside Wrapper() is triggered (this is expected result). What is wrong here?

  • Visual Studio error message is An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in filename.exe.

  • This is happening during call to MyForm.Show() which indirectly calls MyForm_Load() event handler and it calls MyComboBox_SelectedIndexChanged() event handler (and this one indirectly executes the query).

  • If I find and click Wrapper() stack frame during break on exception, I can see the higlighted call to FillMyDataTable() located between Try/Catch.

  • I'm always on main thread (ManagedThreadId is stored during Startup event, then multiple asserts verifying it are inserted in the above code).

  • Stack traces are exactly the same (checked via diff) except the bottommost frame (System.Windows.Forms.NativeWindow.DebuggableCallback() vs. System.Windows.Forms.NativeWindow.Callback()).

    • Topmost frame is System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection).
  • This was not the problem until yesterday. I think I have changed some option.

  • Restart of Visual Studio did not help.

  • There is no change in behavior if Exception assistant in enabled or disabled.

  • Options and exception settings are like this:

  • Stack trace is:

myApp.exe!myApp.frmAPP_PrenosWizard.frmFT_PrenosWizard_Load(Object sender, System.EventArgs e) Line 125 Basic
System.Windows.Forms.dll!System.Windows.Forms.Form.OnLoad(System.EventArgs e) + 0x1d5 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Form.OnCreateControl() + 0x55 bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl(bool fIgnoreVisible) + 0x181 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl() + 0x24 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.WmShowWindow(ref System.Windows.Forms.Message m) + 0x98 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x2b6 bytes 
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) + 0x2a bytes    
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m) + 0x10 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Form.WmShowWindow(ref System.Windows.Forms.Message m) + 0x41 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) + 0x154 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes  
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x57 bytes 
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(int dwExStyle, string lpszClassName, string lpszWindowName, int style, int x, int y, int width, int height, System.Runtime.InteropServices.HandleRef hWndParent, System.Runtime.InteropServices.HandleRef hMenu, System.Runtime.InteropServices.HandleRef hInst, object pvParam) + 0x3c bytes  
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp) + 0x225 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() + 0x125 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Form.CreateHandle() + 0x9f bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get() + 0x45 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value) + 0x160 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.Show() + 0x10 bytes   
myApp.exe!myApp.APP.clsPrenos.ZobrazWizard(myApp.frmAPP_PrenosWizard.enRezimPrenosu rezim, myApp.APP.clsEntita.enEntita zdrojEntita, Integer zdrojID, myApp.APP.clsEntita.enEntita cielEntita, Integer cielID, System.Data.SqlClient.SqlConnection cn1) Line 8212 + 0xa bytes   Basic
myApp.exe!myApp.frmMain.FCreateTransferUI(myApp.frmAPP_PrenosWizard.enRezimPrenosu mode, myApp.APP.clsEntita.enEntita sourceEntity, Integer sourceID, myApp.APP.clsEntita.enEntita targetEntity, Integer targetID, System.Data.SqlClient.SqlConnection cn1) Line 2410 + 0x17 bytes  Basic
myApp.exe!myApp.frmMain.frmMain_Receive(Object sender, myApp.clsFisCommandProcessor.ReceivedEventArgs e) Line 239 + 0xa7 bytes  Basic
myApp.exe!myApp.clsFisCommandProcessor.raise_Received(Object sender, myApp.clsFisCommandProcessor.ReceivedEventArgs e) Line 96 + 0x2e bytes Basic
myApp.exe!myApp.clsFisCommandProcessor.Execute(myApp.clsFisCommand command, Object sender) Line 136 + 0x57 bytes    Basic
myApp.exe!myApp.clsFisCommandProcessor.Execute(myApp.clsFisCommand() commands, Object sender) Line 128 + 0x27 bytes Basic
myApp.exe!myApp.clsFisCommandProcessor.Execute(String scan, Boolean requireMarking, Object sender) Line 122 + 0x30 bytes    Basic
myApp.exe!myApp.frmCommandPad.TSMI_Execute_Click(Object sender, System.EventArgs e) Line 94 + 0x51 bytes    Basic
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.RaiseEvent(object key, System.EventArgs e) + 0x58 bytes 
System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs e) + 0x46 bytes    
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs e) + 0x6e bytes    
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs e, System.Windows.Forms.ToolStripItemEventType met) + 0x83 bytes  
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs e, System.Windows.Forms.ToolStripItemEventType met) + 0x118 bytes    
System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.ProcessCmdKey(ref System.Windows.Forms.Message m, System.Windows.Forms.Keys keyData) + 0x47 bytes   
System.Windows.Forms.dll!System.Windows.Forms.ToolStripManager.ProcessShortcut(ref System.Windows.Forms.Message m, System.Windows.Forms.Keys shortcut) + 0x2dc bytes    
System.Windows.Forms.dll!System.Windows.Forms.ToolStripManager.ProcessCmdKey(ref System.Windows.Forms.Message m, System.Windows.Forms.Keys keyData) + 0x2d bytes    
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0x3c bytes  
System.Windows.Forms.dll!System.Windows.Forms.Form.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0x29 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0x96 bytes   
System.Windows.Forms.dll!System.Windows.Forms.TextBoxBase.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0xda bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessMessage(ref System.Windows.Forms.Message msg) + 0x90 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessControlMessageInternal(System.Windows.Forms.Control target, ref System.Windows.Forms.Message msg) + 0x101 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg) + 0xf6 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg) + 0x5 bytes 
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason, int pvLoopData) + 0x22e bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x177 bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context) + 0x18 bytes 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() + 0x81 bytes    
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() + 0xef bytes   
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[] commandLine) + 0x68 bytes  
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.Assembly assembly, string[] args) + 0x19 bytes 
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel) + 0x6e bytes 
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() + 0x84 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext, string[] activationCustomData) + 0x65 bytes 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext) + 0xa bytes 
mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext) + 0x3e bytes   
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x23 bytes   
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   

解决方案

This question has many moving parts, not sure I can do justice to all of them. Starting point is that you should not subscribe those events if you use a debugger. Wrap that code with If Not System.Diagnostics.Debugger.IsAttached Then so you can properly diagnose and repair unhandled exceptions.

A significant randomizer is what will happen with exceptions that are thrown in a Load event handler. Microsoft had a great deal of trouble properly handling exceptions that are raised while a Windows callback is active. Such as the one that causes the Load event to fire. They changed the rules at Vista, again at Win7, again at Win8. They are different if you run the 32-bit of the 64-bit version. And whether your program runs as a 32-bit or 64-bit process. Using a debugger alters the behavior. The Program Compatibility Assistant may appear when the exception occurs, asking you if you want your program to be "compatible". Everybody says Yes of course, a very bad idea. Adding them up, you can get one of thirty-two different outcomes. Ouch.

It is formally described in this MSDN article but nobody understands it. Not until it doesn't do what they hope for, covered in a question like this one. Mitigating circumstance is that this only bytes when you debug an app, the normal exception back-stop in the message loop ensures this doesn't get out of hand on your user's machine. In other words, what you see when you press Ctrl+F5 or run the program with Explorer is the way you can expect it to work.

This is a problem that affects any GUI program on Windows. But it has a knack of being especially troublesome in a Winforms app, programmers use the Load event entirely too much. Deluded into it by it being the default event for the Form class, it takes but a double-click to generate it. Especially so in VB.NET, it doesn't automatically create the constructor for a form and Visual Basic has a legacy of using the Load event to initialize a form, goes back to VB6.

I normally avoid judging programming practices I see, but using the Load event is today a really rather bad practice because of these problems. Always favor the constructor to initialize a class, a rule that any .NET programmer knows, it isn't different for the Form class. There are exceedingly few reasons to have to use Load, covered in this post.

这篇关于在Try / Catch里面,Visual Studio在Unhandled异常中断和终止应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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