Followers

Follow by Email

Cloud Labels

[Fixing] Report Viewer control toolbar in Google Chrome [Solved] How to kill any process using VB.NET ABAP Admin Panel Template adonet Affiliate site Airline Reservation System Project in PHP Ajax Android androidstudio AngularJS aspnet Big Data Blogger Bookmarking Bootstrap 4 C# C#.Net How To: Send email in asp.net using c# cloud computing Computer Programming CRYSTALREPORTS CSS Databases and ADO.NET DevOps Django dotnet E-commerce Website Php MySQL source code free download Employee Leave Management System in PHP/MySql with Full Source Code Firebase Free Sourcecode in PHP Free Asp.net SourceCode Free SourceCode in C# FreeSourcecode FreeSourcecodevb.net FreeSourcecodinPHP fullform google Google Fuchsia Hospital Management System with C# with Full Source Code How To Assign a Value to a Variable in Python How to Bind Data to Repeater Control in Asp.net How to generate QR Code in C# windows application HOW TO GENERATE RANDOM NUMBER IN C# how to ping ip address in asp net How To Read A Text File In C# How to Use Anchoring Controls In visual c#.net How To Using LINQ to SQL Classes With Example HTML HTML Head Title Access Denied Image Submission Sites introduction to csharp programming JavaProject javascript jQuery Library Management System Using PHP and MySQL with Full Source Code LINQ LINQ In C#.Net MangoDB Microsoft Mobile Blazor mvc MySQL Nodejs NuGet Online Blood Bank Management System Project Online Class and Exam Scheduling System in PHP PhotoshopCs5 PHP pm mandhan yojana PMYOJANA Project python QR (Quick Response) Code Generator using C#.NET Querying a Database with LINQ to SQL sap SAP Basis And Security SAP ECC 6.0 Installation Step by Step SAPMM SarkariNukari sbi School Fees Payment System in PHP With Source code SearchEngine Site seo SEO Tutorials software sql SQLite StringBuilder Class In C# TechNews Timer ajax control In Asp.net Top Most Popular Social Bookmarking TRICK TIPS VB.NET VB.NET Classes VB.NET Conditional Statements VB.NET Loops vbnet visual studio download What are variables? what is JavaScript? What is Link Building? What is Node.Js? whatsappgb Windows Forms WordPress

Labels

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: