注册 登录
编程论坛 VB.NET论坛

tcpclient.GetStream.beginread回调函数的疑问

picat 发布于 2008-10-29 18:22, 2633 次点击
有这样一段程序:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim client As TcpClient = New TcpClient("las.sinica.edu.tw", 23)
        Dim reader As StreamReader = New StreamReader(client.GetStream(), Encoding.Default)

        client.GetStream().BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing)

        rtxtCmd.Text = client.GetStream().BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing).AsyncState
    End Sub
    Private Sub DoRead(ByVal ar As IAsyncResult)
        Dim bytesRead As Integer
        Dim strMessage As String
        Try
            bytesRead = client.GetStream.EndRead(ar)
            If bytesRead < 1 Then
                MessageBox.Show("Disconnected!")
                Exit Sub
            End If
            strMessage = Encoding.ASCII.GetString(readBuffer, 0, bytesRead - 2)
            client.GetStream.BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

其中GetStream.beginread方法的回调函数是DoRead,所以每次Form1 load的时候BeginRead方法完成之后会调用DoRead,但是DoRead过程中的BeginRead方法的回调函数也是DoRead,这样不是会陷入死循环吗? 请高手解答,谢谢
0 回复
1