【发布时间】:2009-10-29 18:02:58
【问题描述】:
这真的让我很困惑,因为我没有对字符串做任何事情。
这是调试器返回给我的详细信息:
System.FormatException 未处理 Message=输入字符串的格式不正确。 源=System.Windows.Forms 堆栈跟踪: 在 System.Windows.Forms.Control.MarshaledInvoke(控制调用者,委托方法,对象 [] 参数,布尔同步) 在 System.Windows.Forms.Control.Invoke(委托方法,对象 [] 参数) 在 C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\Receiver\Receiver\Class1.vb:line 28 中的 Receiver.Class1.CrossThreadAddControl(Control ControlToAdd, Control BaseControl) 在 C:\Users\Jonathan\documents\visual studio 2010\Projects\Receiver\Receiver\ContactList.vb:line 25 中的 Receiver.ContactList.AddContact(Contact user) 在 C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\Receiver\Receiver\Form1.vb:line 45 中的 Receiver.Form1.MySub(IAsyncResult ar) 在 System.Net.LazyAsyncResult.Complete(IntPtr userToken) 在 System.Net.ContextAwareResult.CompleteCallback(对象状态) 在 System.Threading.ExecutionContext.runTryCode(对象 userData) 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,对象 userData) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Net.ContextAwareResult.Complete(IntPtr userToken) 在 System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken) 在 System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 错误代码,UInt32 numBytes,NativeOverlapped* nativeOverlapped) 在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 错误代码,UInt32 numBytes,NativeOverlapped* pOVERLAP) 内部异常:
基本上在 usercontrol(ContactList) 中有一个名为 AddContact 的 sub,它接受 3 个字符串并将它们放入另一个 USerControl (Contact),然后将 Contact 添加到 ContactList 联系人列表在主窗体上,而 AddContact Sub 是从不同的线程启动的,这就是需要 Invoke 的原因。
Public Class ContactList
Sub AddContact(ByVal user As Contact)
If Me.Controls.Count = 0 Then
user.Location = New Drawing.Point(0, 0)
Else
user.Location = New Drawing.Point(0, Me.Controls.Count * 20)
End If
user.Width = Me.Width
user.Displayname = user.Username
For Each UC As Control In Me.Controls
If TypeOf UC Is Contact Then
If CType(UC, Contact).Username = user.Username Then
user.Displayname = user.Username & "@" & user.PCname
End If
End If
Next
Class1.CrossThreadAddControl(user, Me)
End Sub
End Class
这是带有 2 个星号的行(代码中没有实际出现),这显然是导致问题的原因
Shared Sub CrossThreadAddControl(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
If BaseControl.InvokeRequired Then
Dim d As New AddUserD(AddressOf AddUser)
**BaseControl.Invoke(d, ControlToAdd, BaseControl)**
End If
End Sub
Delegate Sub AddUserD(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
Shared Sub AddUser(ByVal ControlToAdd As Control, ByVal BaseControl As Control)
BaseControl.Controls.Add(ControlToAdd)
End Sub
那么知道为什么它说输入字符串的格式不正确吗? (哦,如果我捕获了异常(使用 Try 和 Catch)并且不在 catch 部分中写入任何内容,它就会继续并且它可以正常工作而不会中断。
【问题讨论】:
标签: vb.net string format invoke