|
-
Oct 28th, 2015, 03:59 PM
#1
Thread Starter
Member
Populate Tree View w/ Progress Bar (Background Worker? Multithreading?)
Hi guys,
I've tried all sorts of tutorials, research etc to get this working but have completely run out of ideas now so really need your help.
I have a TreeView which I populate with a list of files, however this can take quite a while sometimes, depending on how many files you have.
Code:
Public Sub GetFilesAndFolders(ByVal fileDirectory As String, ByVal parentNode As TreeNode)
Try
'Get list of all sub folders
Dim lstrFolders() As String = IO.Directory.GetDirectories(fileDirectory)
'If this folder has some folders we need to parse them into the treeview
Dim lobjChildNode As TreeNode = Nothing
If lstrFolders.Count <> 0 Then
For Each ldirFolder In lstrFolders
'Setup child node
lobjChildNode = New TreeNode
lobjChildNode.ImageIndex = 0
lobjChildNode.SelectedImageIndex = 0
lobjChildNode.Text = Path.GetFileName(ldirFolder)
lobjChildNode.Name = Path.GetFileName(ldirFolder)
'Add node to parent
parentNode.Nodes.Add(lobjChildNode)
'Parse this file/folder
GetFilesAndFolders(ldirFolder, lobjChildNode)
Next
Else 'it's a file
Dim lstrFiles() As String = IO.Directory.GetFiles(fileDirectory)
For Each file In lstrFiles
'We only care about sound files
If IsAudioFile(file) Then
Dim lobjNewNode As ctlMP3FileNode = New ctlMP3FileNode
lobjNewNode.filePath = file
lobjNewNode.id3Info = New ID3TagLibrary.MP3File(file)
lobjNewNode.Text = lobjNewNode.id3Info.Title
'if no title in id3 tag we don't want a node with no text
If lobjNewNode.Text = "" Then
lobjNewNode.Text = Path.GetFileName(file)
End If
lobjNewNode.ImageIndex = 1
lobjNewNode.SelectedImageIndex = 1
lobjNewNode.ContextMenuStrip = rcmFile
Dim name As String = lobjNewNode.id3Info.Title & lobjNewNode.id3Info.Artist
lobjNewNode.Name = Replace(name, " ", "")
parentNode.Nodes.Add(lobjNewNode)
'GetFilesAndFolders(file, lobjChildNode, audioContextMenu)
End If
Next
End If
Catch ProbablyNotAFolder As IOException
'ignore it
Catch AccessIssues As UnauthorizedAccessException
parentNode.Nodes.Add(AccessIssues.Message)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
Now this works fine, but I really need to show a progress bar so the user knows the application is still responding.
I have tried putting this into a Background Worker and a different thread but always get a cross thread error, and I've tried Me.CheckForIllegalCrossThreads = False and Control.CheckForIllegalCrossThreads = False
Unfortunately because of the many different thinks I have tried I don't have any code around this to show, if someone could explain with some snippets how I could do this, that would be great.
If you need any further information I will get back to you ASAP.
Any advice is greatly appreciated, thanks in advance!
Last edited by iProRyan; Oct 28th, 2015 at 05:26 PM.
Reason: Some errors in my function to populate treeview I forgot to correct after editing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|