VB.Net TreeView Tutorial on Multithreading

VB.Net TreeView Tutorial on Multithreading Need of Multi threading in vb.net treeview,[Solved]How to kill any process using VB.NET,VB.Net TreeView,VB
Share it:

VB.Net TreeView Tutorial on Multithreading | Need for Multithreading 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 a few minutes to be completed, I can't 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 processes in parallel.

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

Read Also :-[Solved]How to kill any process using VB.NET

Invoke the threads to communicate between them

While working on Windows forms controls in a multithreaded environment, sharing resources between threads has to be taken carefully. Or else an 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 the 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 accepts the arguments named FileInCurrentPath, current node.

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
Share it:

VB.NET

Windows Forms

Post A Comment:

0 comments: