定義委託然後在DataReceived事件中處理(不需要延時)Delegate Sub SetTextCallback(ByVal InputString As String) Private Sub ShowString(ByVal comData As String) txt_Rect.Text += comData "將收到的資料入接收文字框中 txt_Rect.SelectionStart = txt_Rect.Text.Length txt_Rect.ScrollToCaret() End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim inData As String = SerialPort1.ReadExisting Dim d As New SetTextCallback(AddressOf ShowString) BeginInvoke(d, inData) End Sub
定義委託然後在DataReceived事件中處理(不需要延時)Delegate Sub SetTextCallback(ByVal InputString As String) Private Sub ShowString(ByVal comData As String) txt_Rect.Text += comData "將收到的資料入接收文字框中 txt_Rect.SelectionStart = txt_Rect.Text.Length txt_Rect.ScrollToCaret() End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim inData As String = SerialPort1.ReadExisting Dim d As New SetTextCallback(AddressOf ShowString) BeginInvoke(d, inData) End Sub