关于tcpclient.getstream.beginread中回调函数的疑问

picat 2008-10-29 05:54:53
有这样一段程序:
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,这样不是会陷入死循环吗? 请高手解答,谢谢
...全文
341 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ming351208 2010-06-30
  • 打赏
  • 举报
回复
不会,因为有
If bytesRead < 1 Then
MessageBox.Show("Disconnected!")
Exit Sub
End If

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧