0

I'm trying to get the text stored in a rich text box that was created on the main thread. I'm not sure how to make this work with a function, could anyone point me in the right direction? Currently, I cant get anything to be stored in OldMessages. I already start the thread when the main form is loaded.

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
Semonoir
  • 39
  • 6
  • Not sure what type of multithreading approach you are using (your code shows two thread-related elements, `Thread.Sleep` and `.Invoke`, which do not necessarily imply multithreading). If your ideas are not too clear you should better do some research/tests about `Backgroundworker`, which is the easiest 2-thread approach (i.e., ideal for dealing with interface + something else). In any case bear in mind that multithreading is not a must-have, but something to be included only when required. For example: certain code takes too long and avoids the GUI to be updated (and consequently gets frozen). – varocarbas Dec 13 '15 at 15:13
  • @varocarbas The thread is a background worker. I need another thread to do this task as it checks for new messages in the background. – Semonoir Dec 13 '15 at 15:36
  • If you are using the backgroundworker (by the way; backgroundworker represents a second thread already, the GUI thread is the first one) already, you are using it wrong. I recommend you to do some research/tests to get clearer ideas. In fact, I have recently answered a question of another asker with not too clear ideas here: http://stackoverflow.com/questions/34153497/how-to-call-a-control-method-from-another-thread/34154582#34154582 It is in C# and perhaps you don't understand it well, although there are various comments which should be clear enough anyway. – varocarbas Dec 13 '15 at 15:38
  • @varocarbas Ok I will look into background worker but even with the background worker I will have this problem where I will need to access an object from a different thread? – Semonoir Dec 13 '15 at 15:50
  • The basic idea (more or less explained in the aforementioned link and in the official references) is that you have always 1 thread, the main/GUI one. If some of your calculations take too long (e.g., a loop), the main thread has to wait for this to finish and thus cannot perform the regular GUI-updating actions. To avoid that you rely on the backgroundworker which sets a second thread, parallel to the main one (so the GUI can be updated while the given code is executed). There are some rules when communicating between threads; for example: you cannot modify directly a GUI... – varocarbas Dec 13 '15 at 16:02
  • ... element (i.e., any control in your form) from the backgroundworker thread (or any other thread). What makes the backgroundworker so easy to use is that it implements methods/events helping to communicate between both threads, but you have to know how to use them (when you are in which thread, etc.). The ideas are not too difficult (and will help you understand multithreading better) but neither completely straightforward and you have to get them properly. As said: do some research/reading/testing and understand how backgroundworker should be used. – varocarbas Dec 13 '15 at 16:04
  • @varocarbas Okay I have got my background worker and its working however it doesnt loop. Is it safe to nest the dowork handle in a do while loop? – Semonoir Dec 13 '15 at 16:29
  • It is not clear what you are asking, but it seems that you are still not understanding the situation properly. Additionally, this has been quite offopic since a while ago (i.e., you are plainly trying to learn/solve your specific problems; not to ask specific and clear enough doubts which might be useful for others). Do what I suggested (do tests, research and learning) or, eventually, update your question or ask a new one (by including descriptive enough code). But my help will stop here (my intention was just writing 1-2 comments, not a whole chat). – varocarbas Dec 13 '15 at 16:37
  • @varocarbas Did you even read the code? If you read it you would understand what I am trying to do. It is a messaging system where I want a thread to periodically check for new messages for the user. Of course I want this thread to stay alive during the time the main thread is alive, so my question now is, is it safe to loop the dowork handle code. – Semonoir Dec 13 '15 at 16:40
  • I don't like your tone, but I will ignore it just once (and as an exception, almost to compensate my lack of patience with another person yesterday). Again: what you are saying right now (the words which you are using) is transmitting me that you haven't got the idea right (and in any case is not clear what you are asking). This is what I will do in your case: set up a backgroundworker (`bgw`), add the events I want (at least `DoWork`), call all the code I want to be executed in the backgroundworker thread from the `DoWork` event and make sure that any modification in GUI elements from there.. – varocarbas Dec 13 '15 at 16:49
  • ... uses something like what I am showing in the aforementioned question (for example: rely on the `ProgressChanged` event). That is: `Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load bgw.WorkerReportsProgress = True bgw.RunWorkerAsync() End Sub Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork UpdateSystem() End Sub` And update some parts of `UpdateSystem` to rely on `ProgressChanged`. If you still don't understand what I mean, do whatever you want but stop asking me. – varocarbas Dec 13 '15 at 16:52

0 Answers0