输入字符串的格式不正确

时间:2009-10-29 18:02:58

标签: vb.net string format invoke

这让我很困惑,因为我没有对Strings做任何事情。

这是调试器给我的详细信息:

  

System.FormatException未处理     消息=输入字符串格式不正确。     来源= System.Windows.Forms的     堆栈跟踪:          在System.Windows.Forms.Control.MarshaledInvoke(Control caller,Delegate方法,Object [] args,布尔同步)          在System.Windows.Forms.Control.Invoke(Delegate方法,Object [] args)          在Receiver.Class1.CrossThreadAddControl(Control ControlToAdd,Control BaseControl)中的C:\ Users \ Jonathan \ Documents \ Visual Studio 2010 \ Projects \ Receiver \ Receiver \ Class1.vb:第28行          在C:\ Users \ Jonathan \ documents \ visual studio 2010 \ Projects \ Receiver \ Receiver \ ContactList.vb中的Receiver.ContactList.AddContact(联系用户):第25行          at Receiver.Form1.MySub(IAsyncResult ar)在C:\ Users \ Jonathan \ Documents \ Visual Studio 2010 \ Projects \ Receiver \ Receiver \ Form1.vb:第45行          在System.Net.LazyAsyncResult.Complete(IntPtr userToken)          在System.Net.ContextAwareResult.CompleteCallback(对象状态)          在System.Threading.ExecutionContext.runTryCode(Object userData)          在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,Object 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 errorCode,UInt32 numBytes,NativeOverlapped * nativeOverlapped)          在System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * pOVERLAP)     InnerException:

基本上在usercontrol(ContactList)中有一个名为AddContact的子,它接受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部分写任何东西它只是继续并且它正常工作而不会破坏。

1 个答案:

答案 0 :(得分:2)

.Invoke替换为.BeginInvoke.EndInvoke以获取真实的堆栈跟踪。 (仅用于调试目的,您可以稍后再更改。请参阅here。)