[.NET] Textbox 內容消失

作者: Xavier (X.avi(er))   2016-04-25 17:05:35
大家好!
我寫一個 SERVER SOCKET接收資料,
Dim ListenThread As New Thread(AddressOf StartListen)
ListenThread.IsBackground = True
ListenThread.Start()
////////////////////////////////////////////////////
Private Sub StartListen()
Try
serverSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim serverIP As IPAddress = IPAddress.Parse(txtip.Text)
Dim Port As String = txtport.Text
Dim serverhost As New IPEndPoint(serverIP, Int32.Parse(Port))
serverSocket.Bind(serverhost)
serverSocket.Listen(50)
UpdateText("Server started at: " + serverIP.ToString() + ":" +
Port)
Do
ClientSocket = serverSocket.Accept()
If ClientSocket.Connected = True Then
Dim ReceiveThread As New Thread(AddressOf ReceiveData)
ReceiveThread.IsBackground = True
ReceiveThread.Start()
End If
Loop
Catch ex As Exception
UpdateText(ex.StackTrace.ToString())
End Try
////////////////////////////////////////////////////////
Private Sub ReceiveData()
Dim myNetworkStream As New NetworkStream(ClientSocket)
Dim InBytesCount As Integer = 0
Dim myReceiveBytes(1023) As Byte
Dim i As Integer = 0
While True
Try
InBytesCount = myNetworkStream.Read(myReceiveBytes, 0,
myReceiveBytes.Length)
System.Threading.Thread.Sleep(100)
If InBytesCount = 0 Then
Exit While
End If
UpdateText(Encoding.GetEncoding(950).GetString(myReceiveBytes).
TrimEnd().TrimStart())
Catch ex As Exception
MessageBox.Show(ex.ToString)
Exit Sub
End Try
End While
End Sub
//////////////////////////////////////////////////////////////
Private Sub UpdateText(ByVal comData As String)
'如果呼叫txtReceive的是另外的執行緒,傳回True
If Me.TxtReceive.InvokeRequired Then
'利用委派型別建立委派物件,並指定委派的函式
Dim d As New SetTextCallback(AddressOf GetUpdateText)
'用大括號 {} 括住初始值,藉以初始化陣列的值。
Try
Me.Invoke(d, New Object() {comData})
'以指定的引數清單叫用函式
Catch ex As Exception
End Try
Else '相同的執行緒
GetUpdateText(comData) '將收到的資料填入接收文字框中
End If
End Sub
///////////////////////////////////////////////////////////////
Private Sub GetUpdateText(ByVal comData As String)
txtRecord.Text = comData + vbCrLf + txtRecord.Text
End Sub
然後我用手機當CLIENT發送幾道指令,EX. (STX)123456(ETX)
結果 txtRecord 只會顯示當下收到的指令,
而沒有印出所有指令,
請問要怎麼修改才能印出所有指令呢?
感謝!
作者: Marty (DNA探針)   2016-04-25 17:12:00
Textbox屬性改成可以顯示多行?
作者: Xavier (X.avi(er))   2016-04-25 17:14:00
已經是多行了! 3QStartListen()裡面也會印資料,也會疊加舊內容唯獨 ReceiveData()裡印資料之後卻只剩最新的一行不知道是為什麼...orz
作者: niwat (原來如此)   2016-04-26 09:36:00
加入application.doevent會不會有用?

Links booklink

Contact Us: admin [ a t ] ucptt.com