Threading and updating form question.

  • Thread starter Thread starter Roger
  • Start date Start date
R

Roger

I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?


Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDirectories("P:\")
For Each dir In dirs
Dim t As System.Threading.Thread
dim oListView as new SubClass1
t = New System.Threading.Thread(AddressOf
oListView.FileListToListView)
oListView.sFolder = dir
'''' I thought by passing the Treeview1 control I could update
it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Value = 100 * (i / CLng(UBound(dirs)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListView
'''' **** Here is where I want to update the Form1.treeview1 control
tview.nodes.add(sFolder)
end sub
end class
 
Hi Roger,

If I have understand your question, you should use something similar to:
[...]
dim oListView as new SubClass1
oListView.tView = Me.Treeview1
t = New System.Threading.Thread(AddressOf oListView.FileListToListView)
[...]

I hope that helps.

King Regards,

Jorge Serrano Pérez
MVP VB.NET
 
Use delegates:

Delegate Sub TreeViewAddNodeDelegate(ByVal sNode As String)
Private AddNode As New TreeViewAddNodeDelegate(AddressOf AddTvNode)

Private Sub AddTvNode(ByVal sNode As String)
TreeView1.Nodes.Add(sNode)
End Sub

Then use the Invoke method to marshal the call from the executing thread to
the main UI thread:

TreeView1.Invoke(AddNode, New Object() {"Node1"})


hope that helps..
Imran.
 
When I add this I get the following error....

Additional information: The action being performed on this control is being
called from the wrong thread. You must marshal to the correct thread using
Control.Invoke or Control.BeginInvoke to perform this action.

Roger



"Jorge Serrano [MVP VB]"
Hi Roger,

If I have understand your question, you should use something similar to:
[...]
dim oListView as new SubClass1
oListView.tView = Me.Treeview1
t = New System.Threading.Thread(AddressOf oListView.FileListToListView)
[...]

I hope that helps.

King Regards,

Jorge Serrano Pérez
MVP VB.NET



Roger said:
I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?


Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDirectories("P:\")
For Each dir In dirs
Dim t As System.Threading.Thread
dim oListView as new SubClass1
t = New System.Threading.Thread(AddressOf
oListView.FileListToListView)
oListView.sFolder = dir
'''' I thought by passing the Treeview1 control I could update
it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Value = 100 * (i / CLng(UBound(dirs)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListView
'''' **** Here is where I want to update the Form1.treeview1 control
tview.nodes.add(sFolder)
end sub
end class
 
Back
Top