Hello all,
As I'm new (both to VB.net and the forum) my apologies upfront for not using the correct terms, I'm reading up as fast as I can but will need some help and guidance.
Now for the question: I'm trying to fill a TreeView in the background using the BackgroundWorker. My code worked as expected before moving it to the BackgroundWorker after which I immediately encountered the "Action being performed on this control is being called from the wrong thread...." error.
I used the guidance from the "How to; invoke a Delegate Method from: How to: Invoke a Delegate Method but can't get it to work. The "wrong thread" error has disappeared but instead my code now seems to "hang" at the actual adding of the TreeNode to the TreeView (line 11 of the code below:
FRM_BrowseRegistry.TV_Registry.Nodes.Add(NodeName)
I’m hoping somebody can point me in the right direction (again keeping in mind I’m a newbie at programming)
Thank you in advance, Ruud.
Update: On "post new thread" the code listing collapsed, new try.
As I'm new (both to VB.net and the forum) my apologies upfront for not using the correct terms, I'm reading up as fast as I can but will need some help and guidance.
Now for the question: I'm trying to fill a TreeView in the background using the BackgroundWorker. My code worked as expected before moving it to the BackgroundWorker after which I immediately encountered the "Action being performed on this control is being called from the wrong thread...." error.
I used the guidance from the "How to; invoke a Delegate Method from: How to: Invoke a Delegate Method but can't get it to work. The "wrong thread" error has disappeared but instead my code now seems to "hang" at the actual adding of the TreeNode to the TreeView (line 11 of the code below:
FRM_BrowseRegistry.TV_Registry.Nodes.Add(NodeName)
I’m hoping somebody can point me in the right direction (again keeping in mind I’m a newbie at programming)
Thank you in advance, Ruud.
Imports Microsoft.Win32 Public Class FRM_BrowseRegistry 'Create a Delegate Delegate Sub TV_RegistryAddNodeDelegate(ByVal NodeName As TreeNode) 'Declare a class that contains a method with the same signature as the delegate Public Class ShowtreeNodeClass Sub ShowTreeNodeMethod(ByVal NodeName As TreeNode) FRM_BrowseRegistry.TV_Registry.Nodes.Add(NodeName) End Sub End Class Public Sub FRM_BrowseRegistry_Load(sender As Object, e As EventArgs) Handles MyBase.Load BackgroundWorker1.RunWorkerAsync() End Sub Public Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 'Get registrykey for HKEY_CLASSES_ROOT' Dim vRegKeyClassesRoot As RegistryKey = Registry.ClassesRoot 'Create TreeNode and get its child nodes in CreateNodes method ' Dim ClassesRootParentNode As New TreeNode(vRegKeyClassesRoot.Name) ClassesRootParentNode = CreateNodes(ClassesRootParentNode, vRegKeyClassesRoot) 'Show the node on treeview, use invoke as the treeview is updated from a background thread Dim MyShowTreeNodeClass As New ShowtreeNodeClass 'Create an instance of the delegate Dim MyShowTreeDelegate As TV_RegistryAddNodeDelegate = AddressOf MyShowTreeNodeClass.ShowTreeNodeMethod MyShowTreeDelegate.Invoke(ClassesRootParentNode) End Sub Private Function CreateNodes(ByVal vParentNode As TreeNode, ByVal vRegKey As RegistryKey) As TreeNode For Each vSubKeyName As String In vRegKey.GetSubKeyNames() Try ' Open subkey and create a childnode with subkeys name on it ' ' Then create childnodes for childnode ' Dim vSubKey As RegistryKey = vRegKey.OpenSubKey(vSubKeyName, False, Security.AccessControl.RegistryRights.ReadKey) Dim vChildNode As New TreeNode(vSubKey.Name) vChildNode = CreateNodes(vChildNode, vSubKey) vParentNode.Nodes.Add(vChildNode) Catch ex As Security.SecurityException ' Lots of security exceptions will be thrown if user is not admin or doesnt have access for whole registry ' Catch ex As Exception End Try Next Return vParentNode End Function End Class 'FRM_BrowseRegistry
Update: On "post new thread" the code listing collapsed, new try.
Last edited by a moderator: