0

適切なタイトルを思い付くのに十分なほどこの状況を理解しているかどうかさえわかりません。私は VB6 についてある程度理解しており、VB 2010 については急な学習曲線を登る必要があります。

Enterprise iPhone アプリと通信するマルチクライアント サーバー プログラムを作成しようとしています。http://www.strokenine.com/blog/?p=218に基づいて構築する比較的単純な例を見つけました。アプリで動作するようにコードを十分に変更できましたが、問題が 1 つあります。メソッドがフォームのクラス内で呼び出されているにもかかわらず、項目を追加するフォームのコントロールにアクセスできません。(元のコードでもこれを試してみましたが、同じことを行います。作者がどうやってそれを機能させたのかわかりません。)

問題のコード セグメントは次のとおりです。

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

Case "LOGIN" の下で、コードはログイン ID をリストボックスに追加しようとします。例外がスローされます:「System.Windows.Forms.dll で 'System.InvalidOperationException' 型の最初の例外が発生しました」 [デザイン]。

データは、クライアントがログオンするたびに作成される別のクラスから取得されます。これにより、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 についてよく読んでいますが、メソッドとコントロールが同じクラスにある場合、それは必要でしょうか? それとも、クラスとは何かについて根本的な誤解がありますか?

私が得ることができるどんな助けにも感謝します。

4

1 に答える 1

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)

それは機能しますか?構文を確認してください。入力時にタイプミスがある可能性があります。

于 2012-10-25T18:04:45.893 に答える