Getting Treeview node text from a seperate thread

s1ckOh

Well-known member
Joined
Aug 1, 2011
Messages
68
Location
San Diego, Ca
Programming Experience
Beginner
Hello, I've been trying the following code to get the text from each node in a treeview on form1 (ui thread) from a separate thread in a different class. The msgbox always shows nothing. If someone has a link or an idea on how to go about this, much appreciated.

VB.NET:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Class[/COLOR] [COLOR=#2b91af]something[/COLOR]
[COLOR=blue]
    Public Sub[/COLOR] anything()        
[COLOR=blue]        Dim[/COLOR] xThread [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Threading.[COLOR=#2b91af]Thread[/COLOR]([COLOR=blue]AddressOf[/COLOR] whatever)        
        xThread.Start()[COLOR=blue]    
    End[/COLOR] [COLOR=blue]Sub[/COLOR] [COLOR=blue]    

    Sub[/COLOR] whatever() [COLOR=blue]       
        Dim[/COLOR] test [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]        For[/COLOR] [COLOR=blue]Each[/COLOR] item [COLOR=blue]As[/COLOR] [COLOR=#2B91AF]TreeNode[/COLOR] [COLOR=blue]In[/COLOR] form1.TreeView1.Nodes            
            test &= item.Text[COLOR=blue]       
        Next    [/COLOR]        
        MsgBox(test)
[COLOR=blue]    End[/COLOR] [COLOR=blue]Sub

End Class
[/COLOR]
 
You don't access control from secondary threads, plain and simple. Secondary threads are for background work and anything to do with controls is inherently foreground work. If you need to use the data from the TreeView in some long-running process then gather all the data first and pass it into the secondary thread for processing.

The specific reason that you see a blank message in your case is that the TreeView on the form you're accessing is empty. You're using the default instance of the form and that is thread-specific. That means that you're not using the form that you can see on screen but rather creating a whole new one, on which you have never populated the TreeView.
 
Thank you for the info. As you suggested I gathered the data before passing to the new thread and it worked great. After processing the data, instead of trying to add the nodes from the secondary thread I passed it back to the UI thread when I set a public property, which worked fine, however, It won't add to the TreeView...

Back in the UI thread (form1)...

VB.NET:
    Public xfolder As String
   
    Public Property StrChanged As String
        Get
            StrChanged = xfolder
        End Get
        Set(ByVal NewStr As String)
            If NewStr <> xfolder Then
                xfolder = NewStr
                VarChanged()
            End If
        End Set
    End Property

    Private Sub VarChanged()
        'At this point xfolder is populated with the data passed to it from the secondary thread
        MsgBox(xfolder)

        'However, this still doesn't add the nodes
        For Each item As String In xfolder.Split("|"c)
            TreeView1.Nodes.Add(item)
        Next
    End Sub

Does this mean that I am still trying to use the default instance of the TreeView? Is there absolutely no way to get the data back to the original TreeView that I can see on my form?
 
I would suggest that you use a BackgroundWorker for this. You set it off by calling RunWorkerAsync and passing any data in as an argument. You handle the DoWork event, get your data back from the e.Argument property and then do your work. When you're done, any data that needs to be passed back to the UI gets assigned to the e.Result property. You then handle the RunWorkerCompleted event, get your data back from the e.Result property and update the UI. The DoWork event handler is executed on a secondary thread while the RunWorkerCompleted event is executed on the UI thread.
 
Back
Top