我什至不确定我是否足够了解这种情况来想出一个合适的标题。我来自对 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
在案例“LOGIN”下,代码尝试将登录 ID 添加到列表框。它抛出一个异常:“System.Windows.Forms.dll 中发生'System.InvalidOperationException'类型的第一次机会异常”列表框(所有控件,就此而言)在同一个类中,Server.vb 和 Server.vb [设计]。
数据来自客户端登录时创建的另一个类,这会引发切换回 Server 类的事件:
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 的内容,但是如果方法和控件在同一个类中,那是否有必要?还是我对什么是类有基本的误解?
非常感谢我能得到的任何帮助。