0

I have a recursive function with some loops that I use to populate a TreeView with a list of files and folders. In some situations this can take a bit of time to execute especially when loading a lot of files.

I wish to show a progress bar to show how far through the loading process I am.

I have searched everywhere for this and tried with a BackgroundWorker and am now very lost.

Sorry I'm not really sure what code to share but please let me know if you need to see any of the functions/routines.

The error I get with the background worker is a System.InvalidOperationException "Action being performed on this control is being called from the wrong thread."

Background Worker_DoWork:

For i = 0 To 100
   PopulateTreeView()
   BackgrondWorked.ReportProgress(i)
Next

PopulateTreeView Sub:

Dim node As TreeNode = New TreeNode
node.Text = "Music"
TreeView.Nodes.Add(node)
GetFilesAndFolders("D:\Music", node)

This throws the error described above when trying to add the first node with text "Music"

Any suggestions will be greatly appreciated. Thanks.

Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27
  • Possible duplicate of [How to update GUI with backgroundworker?](http://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker) – Bjørn-Roger Kringsjå Oct 25 '15 at 19:13
  • You might also find this link interesting: [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Bjørn-Roger Kringsjå Oct 25 '15 at 19:19
  • You arent supposed to reference controls in the DoWork thread, only from ReportProgress. A BGW is for something that will take a while, like a long DB query or sifting thru lots of files to find certain ones. Not creating TV nodes. – Ňɏssa Pøngjǣrdenlarp Oct 25 '15 at 20:52
  • Is this copied and pasted from your code? It may be deliberate, but might there be spelling errors? 3rd line says BackgrondWorked.. Just checking. – David Wilson Oct 25 '15 at 21:52

0 Answers0