Vb.net ';System.Collections.Generic.KeyNotFoundException';在调用结束时
我刚刚学会了如何在单独的线程中设置GUIVb.net ';System.Collections.Generic.KeyNotFoundException';在调用结束时,vb.net,multithreading,user-interface,delegates,invoke,Vb.net,Multithreading,User Interface,Delegates,Invoke,我刚刚学会了如何在单独的线程中设置GUI Private myGui As SomeGui Public Class myAsyncState Public a As Boolean = True Public b As Integer = 100 End Class Public Sub Caller() ' myGui = New SomeGui() ' setup myGui.Begin() Dim a as Boolean = F
Private myGui As SomeGui
Public Class myAsyncState
Public a As Boolean = True
Public b As Integer = 100
End Class
Public Sub Caller()
'
myGui = New SomeGui()
' setup
myGui.Begin()
Dim a as Boolean = False
Dim b as Integer = 1
Dim state As myAsyncState = New myAsyncState(a, b)
Dim step1 As New xDelegate(AddressOf xMethod)
Dim callBack As New AsyncCallback(AddressOf xMethod_Callback)
Dim asyncResultTest As IAsyncResult = step1.BeginInvoke(a, b, callBack, state)
End Sub
Private Delegate Sub xDelegate(Byval a as Integer, ByVal b As Boolean)
Public Sub xMethod(Byval a as Integer, ByVal b As Boolean)
End Sub
Private Sub xMethod_Callback(ByVal ia As IAsyncResult)
Dim myAsyncResult As AsyncResult = CType(ia, AsyncResult)
Dim myAsyncMethodCaller As xDelegate = CType(myAsyncResult.AsyncDelegate, xDelegate)
Dim state As myAsyncState = CType(myAsyncResult.AsyncState, myAsyncState)
myAsyncMethodCaller.EndInvoke(ia)
xMethod_Finish(state.a, state.b)
End Sub
Private Sub xMethod_Finish(ByVal a As Integer, ByVal b As Boolean)
If Me.InvokeRequired Then
Invoke(New xDelegate(AddressOf xMethod_Finish), New Object() {a, b}) ' here
' Also tried Invoke(New xDelegate(AddressOf xMethod_Finish), a, b) though the above is what I have seen in documentation
' also tried to make Dim state As myAsyncState = New myAsyncState(a, b) and use it as an argument
Else
yMethod(a, b)
myGui.Finish()
End If
End Sub
我返回并传递值,一切都很好。。。然后我返回到它进行测试,发现一个错误:
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in System.Windows.Forms.dll
Additional information: The given key was not present in the dictionary.
例外情况是在“xMethod”完成之后,在xMethod_Finish中我显示的“here”行上。看起来有一些不匹配的参数-但我认为它们都是正确的-我花了很多精力来理解如何将参数传递给委托,以便在第一个方法的EndInvoke(仍将在GUI线程中)之后,它们也可以传递给后续方法
请帮我看看我做错了什么。谢谢。您的调用看起来是正确的 错误可能出现在
yMethod
或myGui.Finish()
方法中。当异常发生在被调用的方法中时,它们可能会变得有些隐藏。检查异常的InnerException
属性,以获取导致KeyNotFoundException
的更多信息和堆栈跟踪
您可以在问题方法中设置断点来调试错误。您的调用看起来是正确的 错误可能出现在
yMethod
或myGui.Finish()
方法中。当异常发生在被调用的方法中时,它们可能会变得有些隐藏。检查异常的InnerException
属性,以获取导致KeyNotFoundException
的更多信息和堆栈跟踪
您可以在问题方法中设置断点来调试错误。谢谢,我已经解决了这个问题。事实上,调用时出错,加上我缺乏经验,使我花了几个小时试图调试错误的问题……谢谢,我已经解决了。错误发生在调用上,加上我缺乏经验,这让我花了几个小时试图调试错误的问题。。。