I have a few classes. One of the classes handles everything to do with updating controls (Such as adding items to a listview, etc.) and another is handling loops. I'm trying to add something to a richtextbox from another thread. This is my code
in ClsControls:
Loop class:Code:Public Sub doLog(ByVal rtbLog As RichTextBox, ByVal what As String, Optional ByVal clr As String = "black", Optional ByVal indent As Integer = 0, Optional ByVal timestamp As Boolean = True) 'Check if the control needs an invoke If rtbLog.InvokeRequired Then : Dim deleg As New delDoLog(AddressOf doLog) : rtbLog.Invoke(deleg, rtbLog, what, clr, indent, timestamp) : Exit Sub : End If 'Set up variables Dim Time, strAppend As String Dim color As Color = color.FromName(clr) 'Set up message Time = "[" & DateTime.Now.ToShortTimeString & "] : " If timestamp Then : strAppend = Time & what & vbNewLine : Else : strAppend = what & vbNewLine : End If 'Build output With rtbLog .SelectionColor = color .SelectionIndent = indent .AppendText(strAppend) .ScrollToCaret() End With End Sub
I have it set up like this just to see if it works, which obviously, it doesn't. If I put both pieces of code on the main form, however, it works without a problem.Code:Public Sub startThread(ByVal strUri As String, ByVal strFldr As String, ByVal tmr As String, ByVal threadDilim As String) Dim threadName As String Dim mainThread As Thread = New Thread(AddressOf getPageLoop) 'Create a new Thread threadName = strUri & threadDilim & strFldr & threadDilim & tmr 'Set up the variable to be passed to the thread mainThread.Name = threadName 'Pass the info to the thread via the name mainThread.Start() 'Start the thread End Sub Public Sub getPageLoop() Dim try3 As New clsControls try3.doLog(My.Forms.frmMain.rtbLog, "Being called form thread") End Sub
The code executes fine. If I add a breakpoint, everything works how its supposed too, however, the textbox isnt updating.
eg:
http://i51.tinypic.com/29et0cy.jpg
But with no change to the text box.
Whats going on? Any ideas?
XI Wiki

