none
请教c#转vb.net RRS feed

  • 问题

  •       if (sendSocket.Connected)
                {
                    menu_showSend.Enabled = true;

                    ThreadPool.QueueUserWorkItem(new WaitCallback((stateInfo) =>
                        {
                            try
                            {
                                byte[] buf = new byte[1024];
                                int size = 0;
                                do
                                {
                                    size = sendSocket.Receive(buf, 1024, SocketFlags.None);
                                    if (size > 0)
                                    {

                                        ReceivedMessage = Encoding.ASCII.GetString(buf, 0, size);
                                      
                                        this.Invoke(new InvokeDelegate(() =>
                                        {
                                            textBox_MsgLog.Text += "\r\nThe Server said: " + ReceivedMessage;
                                        }));
                                    }

                                } while (size > 0);
                            }
                            catch (SocketException sockEx)
                            {
                                //Skip the exception that was raised when we close the socket while still receiving
                                if (sockEx.ErrorCode != 10004)
                                    MessageBox.Show(sockEx.ErrorCode.ToString() + ":" + sockEx.Message);
                            }
                        }));
                }
                else
                {
                    MessageBox.Show("Cannot connect to server.");
                    menu_Connect.Text = "Connect";
                }
            }

    看不懂怎么一回事

    请教高手

    谢谢


    tfnpghl
    2010年3月26日 11:39

答案

  • Hi,

    大致上应该是这样,

    Public Sub Testing()
        If sendSocket.Connected Then
            menu_showSend.Enabled = True
            
            ThreadPool.QueueUserWorkItem(New WaitCallback(CallBack), stateInfo)
        Else
            MessageBox.Show("Cannot connect to server.")
            menu_Connect.Text = "Connect"
        End If
    End Sub
    
    Public Sub CallBack(ByVal stateInfo As Object)
        Try
            Dim buf As Byte() = New Byte(1023) {}
            Dim size As Integer = 0
            Do
                size = sendSocket.Receive(buf, 1024, SocketFlags.None)
                If size > 0 Then
                    
                    ReceivedMessage = Encoding.ASCII.GetString(buf, 0, size)
                    
                    Me.Invoke(New InvokeDelegate(InvokeMethod))
                    
                End If
            Loop While size > 0
        Catch sockEx As SocketException
            'Skip the exception that was raised when we close the socket while still receiving
            If sockEx.ErrorCode <> 10004 Then
                MessageBox.Show((sockEx.ErrorCode.ToString() & ":") + sockEx.Message)
            End If
        End Try
    End Sub
    
    Public Sub InvokeMethod()
        textBox_MsgLog.Text += vbCr & vbLf & "The Server said: " & ReceivedMessage
    End Sub
    

     

    因为VB.NET不支持lamda expression 与 anonymous method,所以我改一点C#的代码

            public void Testing()
            {
                if (sendSocket.Connected)
                {
                    menu_showSend.Enabled = true;
    
                    ThreadPool.QueueUserWorkItem(new WaitCallback(CallBack), stateInfo);
                }
                else
                {
                    MessageBox.Show("Cannot connect to server.");
                    menu_Connect.Text = "Connect";
                }
            }
    
            public void CallBack(object stateInfo)
            {
                try
                {
                    byte[] buf = new byte[1024];
                    int size = 0;
                    do
                    {
                        size = sendSocket.Receive(buf, 1024, SocketFlags.None);
                        if (size > 0)
                        {
    
                            ReceivedMessage = Encoding.ASCII.GetString(buf, 0, size);
    
                            this.Invoke(new InvokeDelegate(InvokeMethod));
                        }
    
                    } while (size > 0);
                }
                catch (SocketException sockEx)
                {
                    //Skip the exception that was raised when we close the socket while still receiving
                    if (sockEx.ErrorCode != 10004)
                        MessageBox.Show(sockEx.ErrorCode.ToString() + ":" + sockEx.Message);
                }
            }
    
            public void InvokeMethod()
            {
                textBox_MsgLog.Text += "\r\nThe Server said: " + ReceivedMessage;
            }

    2010年3月26日 15:20