vb.net需要一些线程帮助

时间:2015-12-13 15:06:46

标签: vb.net multithreading richtextbox

我正在尝试将文本存储在主线程上创建的富文本框中。我不确定如何使用函数来完成这项工作,有人能指出我正确的方向吗?目前,我无法将任何内容存储在OldMessages中。我已经在加载主窗体时启动了线程。

Private Delegate Sub UpdateSystemDelegate()

    Private Sub UpdateSystem()
        Do While StopProgram = False
            'MsgBox("This shit works elegiggle")

            Dim OldMessages As String
            Dim NewMessages As String

            Dim Query As String = "SELECT StatusMessage FROM tbluser WHERE UserID=@userID"

            Using Conn As New MySqlConnection(MySQL.ConnectionDetails)
                Using Comm As New MySqlCommand()
                    With Comm
                        .Connection = Conn
                        .CommandText = Query
                        .CommandType = CommandType.Text
                        .Parameters.AddWithValue("@userID", frmLogin.User.UserID)
                    End With
                    Try
                        Conn.Open()
                        Dim Reader As MySqlDataReader = Comm.ExecuteReader()
                        If rtfMessages.InvokeRequired Then
                            OldMessages = rtfMessages.Invoke((New UpdateSystemDelegate(AddressOf rtfMessages_Return)))
                        Else
                            OldMessages = rtfMessages_Return()
                        End If
                        ' MsgBox("old messages: " & OldMessages)
                        MsgBox(OldMessages)
                        While Reader.Read() OrElse (Reader.NextResult And Reader.Read)
                            NewMessages &= Reader.GetString(0)
                        End While
                        'MsgBox("new messages: " & NewMessages)
                        If Not NewMessages.Equals(OldMessages) Then ShowBalloonTip("You have new messages(s) ", "Khan Motors - Messages") ' Else ShowBalloonTip("No new messages(s) ", "Khan Motors - Messages")
                    Catch ex As MySqlException
                        ShowBalloonTip("Could not check for new messages ", "Khan Motors - Today's Task(s)")
                    End Try
                End Using
            End Using

            Thread.Sleep(3000) '180000

        Loop

    End Sub

Private Function rtfMessages_Return() As String
    Return rtfMessages.Text
End Function

0 个答案:

没有答案