Results 1 to 2 of 2

Thread: .net multithread issue     submit to reddit submit to twitter

  1. #1
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    .net multithread issue

    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:
    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
    Loop class:
    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
    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.

    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?

  2. #2
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Just incase anyone (For what ever reason) ever runs into this problem, this is how I solved it.

    The issue was that the form was being created as a new instance from the class.

    Added this to frmMain:
    Code:
        
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Set the control array up
            setControls()
    
            'Sent message to log saying the program started okay
            cntrls.doLog(rtbLog, "Started up.", "green")
        End Sub
    
    Private Sub setControls()
            'This sub is to add the controls to an arraylist. This is a ghetto fix to accessing controls from another thread. Works, but ghetto
            Dim tmpStruct As New clsControls.frmControls
            With tmpStruct : .imgRealtime = imgRealtime : .lstDownloads = lstDownloads : .lstThreads = lstThreads : .rtbLog = rtbLog : End With
            cntrls.arrControls.Add(tmpStruct)
        End Sub
    
        Private Sub btnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
            doBot.setClasses(cntrls, dtaHndle)
            doBot.startAllScan(dtaHndle)
        End Sub
    Added this to the Controls class:
    Code:
        'Frustrated fix to threading problem.
        Public arrControls As New ArrayList
    
        Public Structure frmControls
            Dim rtbLog As RichTextBox
            Dim imgRealtime As PictureBox
            Dim lstThreads As ListView
            Dim lstDownloads As ListView
        End Structure
    To the MainLoop class:
    Code:
        Dim controls As clsControls
        Dim datahandler As Datahandler
        Dim threadDilim As String = ""
    
        Public Sub setClasses(ByVal cntrls As clsControls, ByVal datahandle As Datahandler)
            'Import all the classes used in the main form into the class
            controls = cntrls : datahandler = datahandle : threadDilim = datahandle.threadDilim
        End Sub
    It works.

Similar Threads

  1. microsoft .net framework issue
    By Cream Soda in forum Tech
    Replies: 0
    Last Post: 2010-07-15, 01:05