VB.Net TreeView Multithreading


This article is written by Pon Saravanan  on 07-Dec-09 Last modified on :14-Jun-10





VB.Net TreeView Tutorial on Multithreading

Need of Multi threading in vb.net treeview

I am in the process of developing a windows based car pc Front-End in VB.Net. For the media playback, I was doing a PlayList editor. I don’t want to use the stock PlayList with media player as I need the custom sorting and remove PlayList item while playback.

I was in need of hierarchical control to load the files from various folders. I am a fan of  VB.Net TreeView from Classic VB days. So I thought the VB.Net TreeView is a good component for this. Now the problem is the hard disk contains around 100 GB of songs. Populating them into a tree view took around few minutes to be completed, I cant wait till then without doing anything. So I planned to run loading VB.Net TreeView in a separate thread. Immediately I have decided to go for multi threading as we can still work on other process in parallel.

.Net has excellent support for multi threading as it helps programming much easier than VB6.

Invoke the threads to communicate between them

While working on Windows forms controls in a multithreaded environment, sharing resources between threads   have to be taken carefully. Or else exception will be thrown.

In the current scenario, if a VB.Net TreeView is being updated in a separate thread, then actually the VB.Net TreeView will reside in main thread and nodes will be added in consecutive threads. So here the sharing resources have to be handled properly.

To communicate/share from one thread to another thread we can use Invoke. In the current approach, the nodes have to be added using invoke as following
TreeView1.Invoke(UpdateTreeDelegate, New Object() {FileInCurrentPath, CurrentNode})
As in the previous code segment, the Invoke can be used along with a delegate. The delegate points to a function that does the job and accept the arguments named FileInCurrentPath, CurrentNode.

VB.Net Source Code with ListBox

Imports System.Threading
Public Class Form1
    Dim FilePath
    Dim FolderNode As TreeNode
    Delegate Function UpdateTree(ByVal nodeName As String, ByVal CurrentNode As TreeNode) As TreeNode
    Public UpdateTreeDelegate As UpdateTree
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FilePath = "D:\Pons\bck"
        UpdateTreeDelegate = New UpdateTree(AddressOf AddNode)
        FolderNode = TreeView1.Nodes.Add("Projects")
        FolderNode.ExpandAll()
        Dim thre As New Thread(New ThreadStart(AddressOf LoadTreeView))
        thre.Start()
        LoadListBox(FilePath)
        'LoadTreeView()
    End Sub
    Private Sub LoadTreeView()
        LoadTreeView(FilePath, FolderNode)
    End Sub
    Private Sub LoadTreeView(ByVal CurrentPath As String, ByVal CurrentNode As TreeNode)
        For Each FileInCurrentPath In My.Computer.FileSystem.GetFiles(CurrentPath)
            TreeView1.Invoke(UpdateTreeDelegate, New Object() {FileInCurrentPath, CurrentNode})
            Application.DoEvents()
        Next
        For Each FolderInCurrentPath In My.Computer.FileSystem.GetDirectories(CurrentPath)
            Dim FolderNode = DirectCast(TreeView1.Invoke(UpdateTreeDelegate, New Object() {FolderInCurrentPath, CurrentNode}), TreeNode)
            LoadTreeView(FolderInCurrentPath, FolderNode)
        Next
    End Sub
    Private Function AddNode(ByVal nodeName As String, ByVal CurrentNode As TreeNode) As TreeNode
        Return CurrentNode.Nodes.Add(nodeName)
    End Function
    Private Sub LoadListBox(ByVal CurrentPath As String)
        For Each FileInCurrentPath In My.Computer.FileSystem.GetFiles(CurrentPath)
            ListBox1.Items.Add(FileInCurrentPath)
            Application.DoEvents()
            Label1.Text = "file added " + FileInCurrentPath
        Next
        For Each FolderInCurrentPath In My.Computer.FileSystem.GetDirectories(CurrentPath)
            ListBox1.Items.Add(FolderInCurrentPath)
            LoadListBox(FolderInCurrentPath)
        Next
    End Sub
End Class



« Previous - GridView Sorting Vb.Net
ASP.Net Repeater - Next »







Comments
  • GUEST
    What if I only want to see the folders and subfolders and not the files? 12/22/2011 3:19:47 AM

  • GUEST
    1/2/2012 12:23:31 PM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Gray
Blue
Brown
GraySimple