不更新主要表单richtextbox和文本框的背景工作者

时间:2016-10-31 15:18:43

标签: vb.net multithreading

我正在尝试通过后台工作程序将字符串发送到richtextbox以更新主窗体上的控件。

我从另一个类/线程的串口接收数据,并在启动后台工作程序的主窗体上调用公共方法。

后台工作程序启动但无法更新richtextbox,但确实显示了带有正确序列数据的消息框。

Public Class Form1
    Public WithEvents RichTextBox2 As RichTextBox
    Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
    Public WithEvents TextBox1 As TextBox

    Public Serial_txt As String

    Public Sub Display_Serial_Data_In(ByVal txt As String)
        Serial_txt = txt

        If Me.BackgroundWorker1.IsBusy = False Then Me.BackgroundWorker1.RunWorkerAsync() : MsgBox("Running Worker Proccess")

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.RichTextBox2.AppendText(Serial_txt)
        Me.TextBox1.Text = Serial_txt
        MsgBox(Serial_Txt, vbOKOnly, "Worker Complete")

    End Sub
End Class

0 个答案:

没有答案