在 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
在 Visual Studio 中,错误 SQL 命令中的错误转到
UnhandledExHandler(),
但如果在 VS 外部启动相同的 EXE,则会触发 Wrapper() 内的 Catch ex As Exception(这是预期结果)。这里有什么问题?
Visual Studio 错误消息是An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in filename.exe。
这发生在调用MyForm.Show() 的过程中,它间接调用MyForm_Load() 事件处理程序并调用MyComboBox_SelectedIndexChanged() 事件处理程序(并且这个间接执行查询)。
如果我在异常中断期间找到并单击 Wrapper() 堆栈帧,我可以看到位于 Try/Catch 之间的对 FillMyDataTable() 的高亮调用。
我总是在主线程上(ManagedThreadId 存储在Startup 事件期间,然后在上面的代码中插入了多个验证它的断言)。
-
除了最底部的帧(System.Windows.Forms.NativeWindow.DebuggableCallback() 与 System.Windows.Forms.NativeWindow.Callback())之外,堆栈跟踪完全相同(通过 diff 检查)。
- 最上面的框架是
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)。
直到昨天才出现这个问题。我想我改变了一些选项。
重新启动 Visual Studio 没有帮助。
如果启用或禁用异常助手,行为不会发生变化。
选项和异常设置如下:
堆栈跟踪是:
myApp.exe!myApp.frmAPP_PrenosWizard.frmFT_PrenosWizard_Load(Object sender, System.EventArgs e) 第 125 行基本
System.Windows.Forms.dll!System.Windows.Forms.Form.OnLoad(System.EventArgs e) + 0x1d5 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.OnCreateControl() + 0x55 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl(bool fIgnoreVisible) + 0x181 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl() + 0x24 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.WmShowWindow(ref System.Windows.Forms.Message m) + 0x98 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x2b6 字节
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) + 0x2a 字节
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m) + 0x10 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.WmShowWindow(ref System.Windows.Forms.Message m) + 0x41 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) + 0x154 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 字节
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x57 字节
[本机到托管转换]
[管理到本地转换]
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, 对象 pvParam) + 0x3c 字节
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp) + 0x225 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() + 0x125 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.CreateHandle() + 0x9f 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get() + 0x45 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value) + 0x160 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.Show() + 0x10 字节
myApp.exe!myApp.APP.clsPrenos.ZobrazWizard(myApp.frmAPP_PrenosWizard.enRezimPrenosu rezim,myApp.APP.clsEntita.enEntita zdrojEntita,整数 zdrojID,myApp.APP.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 字节基本
myApp.exe!myApp.frmMain.frmMain_Receive(Object sender, myApp.clsFisCommandProcessor.ReceivedEventArgs e) 第 239 行 + 0xa7 字节基本
myApp.exe!myApp.clsFisCommandProcessor.raise_Received(Object sender, myApp.clsFisCommandProcessor.ReceivedEventArgs e) 第 96 行 + 0x2e 字节基本
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) 第 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 字节
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs e) + 0x6e 字节
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs e, System.Windows.Forms.ToolStripItemEventType) + 0x83 字节
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs e, System.Windows.Forms.ToolStripItemEventType) + 0x118 字节
System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.ProcessCmdKey(ref System.Windows.Forms.Message m, System.Windows.Forms.Keys keyData) + 0x47 字节
System.Windows.Forms.dll!System.Windows.Forms.ToolStripManager.ProcessShortcut(参考 System.Windows.Forms.Message m,System.Windows.Forms.Keys 快捷方式)+ 0x2dc 字节
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 字节
System.Windows.Forms.dll!System.Windows.Forms.Form.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0x29 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0x96 字节
System.Windows.Forms.dll!System.Windows.Forms.TextBoxBase.ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) + 0xda 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessMessage(ref System.Windows.Forms.Message msg) + 0x90 字节
System.Windows.Forms.dll!System.Windows.Forms.Control.PreProcessControlMessageInternal(System.Windows.Forms.Control 目标,参考 System.Windows.Forms.Message msg) + 0x101 字节
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(ref System.Windows.Forms.NativeMethods.MSG msg) + 0xf6 字节
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(参考 System.Windows.Forms.NativeMethods.MSG msg)+ 0x5 字节
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason, int pvLoopData) + 0x22e 字节
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x177 字节
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 上下文) + 0x18 字节
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() + 0x81 字节
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() + 0xef 字节
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[] commandLine) + 0x68 字节
[本机到托管转换]
[管理到本地转换]
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.Assembly 程序集,string[] args) + 0x19 字节
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel) + 0x6e 字节
mscorlib.dll!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() + 0x84 字节
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 字节
mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext) + 0x3e 字节
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x23 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x66 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态) + 0x6f 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 字节