我单步执行这段代码,发现该函数不仅从未被调用,而且 myBase.Load 的其余部分也从未完成这里发生的事情。
现在显示所有外部引用。程序永远不会碰到 ** 中包围的行,并且确实将 frmMain_Load 作为第一项运行。单步执行图标确实落在以 reader= 开头的行上,但从未调用 runAsIsQuery (断点不会捕获,单步执行只会消失)。然后它向我显示 frmMain,而不处理来自 frmMain_Load 或 runAsISQuery 的任何其他代码
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
sqlstring = "SELECT Nickname FROM tblBikeInfo"
reader = sql.runAsIsQuery(cnn, sqlstring) 'never fires
**If 1 = 1 Then**
'ummmm never comes back here either
End If
询问有关 frmMain 中作为全局变量的其他引用的额外详细信息
Dim reader As OleDbDataReader
Dim sql As OLEDB_Handling 'custom class
Public cnn = MotorcyleDB.GetConnection
自定义类的函数 (OLEDB_Handling)
**Public Function runAsIsQuery(connection As OleDbConnection, SQL As String) As OleDbDataReader**
Dim reader As OleDbDataReader
Dim command As New OleDbCommand(SQL)
command.Connection = connection
Try
connection.Open()
reader = command.ExecuteReader()
Return reader
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
名为 (MotorcyleDB) 的连接字符串类
Public Shared Function GetConnection() As OleDbConnection
Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\************\Documents\Visual Studio 2010\Projects\MotorcycleMinder\MotorcycleMinder\MotorcycleServiceLog11.accdb")
End Function
最佳答案
我发现了这个: http://blog.adamjcooper.com/2011/05/why-is-my-exception-being-swallowed-in.html
这是该网站的一个片段。
If these conditions are met:
- You are running on a 64-bit version of Windows (whether your application is built for 32-bit or 64-bit doesn’t matter; only the bit depth of the OS)
- You are building a WinForms app
- You are debugging the application with Visual Studio (using default options for Exception catching)
- Your main form has a Load event handler
- During the execution of your Load handler, an exception occurs
Then:
The exception will be silently swallowed by the system and, while your handler will not continue execution, your application will continue running.If you wrap your handler code in a try/catch block, you can still explicitly catch any thrown exceptions. But if you don’t, you’ll never know anything went wrong.
Note that all of the conditions must be met. If, for instance, you run the application without debugging, then an unhandled exception still be correctly thrown.
网站上还有一个解决方法。但我会将代码放在 try catch block 中,或者将整个代码放在初始化程序/构造函数中。
关于vb.net - 调用函数 vb.net 时 mybase.load 停止加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297145/