【问题标题】:vb.net: listbox.items.add() throws exception in same classvb.net:listbox.items.add() 在同一个类中抛出异常
【发布时间】:2012-10-25 16:02:22
【问题描述】:

我什至不确定我是否足够了解这种情况,无法想出一个合适的标题。我来自对 VB6 的适度了解,并且必须为 VB 2010 攀登陡峭的学习曲线。

我正在尝试创建一个与我的 Enterprise iPhone 应用程序通信的多客户端服务器程序。我在这里找到了一个相对简单的示例:http://www.strokenine.com/blog/?p=218。我已经能够修改代码以使其与我的应用程序一起工作,但有一个小故障:我无法访问表单上的控件来添加项目,即使该方法是在表单的类中调用的。 (我也在原始代码上试过这个,它做同样的事情。我不知道作者是如何让它工作的。)

这是有问题的代码段:

Public Class Server 'The form with the controls is on/in this class.
Dim clients As New Hashtable 'new database (hashtable) to hold the clients


    Sub recieved(ByVal msg As String, ByVal client As ConnectedClient)
    Dim message() As String = msg.Split("|") 'make an array with elements of the message recieved
    Select Case message(0) 'process by the first element in the array
        Case "CHAT" 'if it's CHAT
            TextBox3.Text &= client.name & " says: " & " " & message(1) & vbNewLine 'add the message to the chatbox
            sendallbutone(message(1), client.name) 'this will update all clients with the new message
            '                                       and it will not send the message to the client it recieved it from :)
        Case "LOGIN" 'A client has connected
            clients.Add(client, client.name) 'add the client to our database (a hashtable)
            ListBox1.Items.Add(client.name) 'add the client to the listbox to display the new user
    End Select

End Sub

在“登录”情况下,代码尝试将登录 ID 添加到列表框。它抛出一个异常:“System.Windows.Forms.dll 中发生'System.InvalidOperationException' 类型的第一次机会异常”列表框(就此而言,所有控件)在同一个类中,Server.vb 和 Server.vb [设计]。

数据来自客户端登录时创建的另一个类,这会引发切换回服务器类的事件:

Public Class ConnectedClient

Public Event gotmessage(ByVal message As String, ByVal client As ConnectedClient)    'this is raised when we get a message from the client
Public Event disconnected(ByVal client As ConnectedClient)    'this is raised when we get the client disconnects

Sub read(ByVal ar As IAsyncResult) 'this will process all messages being recieved
    Try
        Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
        Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
        RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents 
        '                               the current client which it has recieved the message from to perform any client specific
        '                               tasks if needed
        cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
    Catch ex As Exception
        Try 'if an error occurs in the reading purpose, we will try to read again to see if we still can read
            Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
            Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
            RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents 
            '                               the current client which it has recieved the message from to perform any client specific
            '                               tasks if needed
            cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
        Catch ' IF WE STILL CANNOT READ
            RaiseEvent disconnected(Me)  'WE CAN ASSUME THE CLIENT HAS DISCONNECTED
        End Try

    End Try
End Sub

我希望我能理解这一切。这一切似乎来回反弹,看起来如此复杂。

我尝试过使用 Me.listbox1 和 Server.listbox1 以及其他几个类似的结构,但无济于事。

我正在阅读很多关于 Invoke 和 Delegates 的内容,但是如果方法和控件在同一个类中,那是否有必要?还是我对什么是类有基本的误解?

非常感谢我能得到的任何帮助。

【问题讨论】:

    标签: vb.net multithreading visual-studio-2010 delegates invoke


    【解决方案1】:
        Private Delegate Sub UpdateListDelegate(byval itemName as string)
        Private Sub UpdateList(byval itemName as string)
        If Me.InvokeRequired Then
            Me.Invoke(New UpdateListDelegate(AddressOf UpdateList), itemName)
        Else
            ' UpdateList
            ' add list add code
            ListBox1.Items.Add(itemName)
        End If
        End Sub
    

    在上面添加,然后替换:

       ListBox1.Items.Add(client.name)
    

       UpdateList(client.name)
    

    有效吗?检查语法,我输入时可能有错字。

    【讨论】:

    • 明白了!起初它给我带来了一些麻烦,但这是因为我用于调试问题的代码中的其他一些东西。去掉那个东西,它是金色的!谢谢!还有其他解决列表框的方法(如删除项目),但现在我对这个过程有了更好的理解。再次感谢!
    • 这是因为server类中的received()在UI线程之外的另一个线程中。
    • 我想让我感到困惑的是 Received() 位于 server.vb 中,它是 UI 的“基地”,也就是列表框所在的位置。根据我的 VB6 经验,只要调用代码在同一个类中(根据我对类是什么的理解),它仍然应该进行通信。如何判断一个例程是否在不同的线程上运行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2022-06-30
    相关资源
    最近更新 更多