Threading and Having a class update a form control Problem....

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

LVL 8
Rog DManager Inforamtion SystemsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KGregCommented:
The following code goes in your main form:

    Delegate Sub UpdateTreeviewHandler(ByVal sFolder As String)

    Private Sub UpdateTreeview_Callback(ByVal sFolder As String)
        If Me.InvokeRequired Then
            '*** switch over to primary UI thread
            Dim handler As New UpdateTreeviewHandler(AddressOf UpdateTreeview_Implementation)
            Dim args() As Object = {sFolder}
            Me.BeginInvoke(handler, args)
        Else
            '*** direct call - already running on primary UI thread
            UpdateUI_Implementation(sFolder)
        End If
    End Sub

    Private Sub UpdateTreeview_Implementation(ByVal sFolder As String)
        MyTreeview.Nodes.Add(sFolder)
        Application.DoEvents()
    End Sub

This next class will be the object that holds a delegate to the callback functions:

Public Class MyThreadClass
    Private delHandler As frmMain.UpdateTreeviewHandler

    Public Sub New(ByVal delHandler As frmMain.UpdateTreeviewHandler)
        Me.delHandler = delHandler
    End Sub

    Public Sub Process()
        '
        ' This is the code your thread performs..
        '
        delHandler.BeginInvoke(sFolder, Nothing, Nothing)
    End Sub
End Class


Finally,  on the form, do this to create the thread:

dim objThread as Threading.Thread
dim objProcess as new MyThreadClass(New UpdateTreeviewHandler(AddressOf UpdateTreeview_Callback)

objThread = New Threading.Thread(AddressOf objProcess.Process)


Hope this helps!

KGREG

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
What your doing should work though.  Try throwing in a Refresh and DoEvents after you update the TreeView:

    tview.nodes.add(sFolder)
    tview.Refresh()
    Aplication.DoEvents()

~IM
0
KGregCommented:
Now, let me explain why this works.

You can't pass the treeview to the thread, because there is no way to pass it by reference.  The thread and the form that owns the treeview are two different processes in a sense.  What you can do, is pass a delegate object to a constructor of a class - which allows another object to invoke function calls on behalf of your form.

After that, all we have to do is start a thread based on our second object (which has the delegate object) and when the time comes to update the treeview, we invoke the delegate - which will call the function on the form.

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Rog DManager Inforamtion SystemsAuthor Commented:
kGreg,


This works great, but one problem is the application closes automatically when Run.  I am not calling anything to cause a close of application.  Any suggestions.  Do I need to have some clean up code when the threads are done?
I am very new to threading.

''  *** Here is where I call start the threads
Public Sub GetDirs()
        Dim dir As String
        Dim i As Integer = 1
        Dim dirs() As String = Directory.GetDirectories("P:\")
        ProgressBar1.Maximum = dirs.Length
        For Each dir In dirs
            Dim objThread As Threading.Thread
            Dim objProcess As New MyThreadClass(New UpdateTreeviewHandler(AddressOf UpdateTreeview_Callback))
            objThread = New Threading.Thread(AddressOf objProcess.Process)
            objProcess.sFolder = dir
            objThread.Name = dir
            objThread.Start()
            i += 1
            ProgressBar1.Value = ProgressBar1.Value + 1
        Next
    End Sub







'''''' ****  THis was added to the form code as explained above....

 Delegate Sub UpdateTreeviewHandler(ByVal sFolder As String)

    Private Sub UpdateTreeview_Callback(ByVal sFolder As String)
        If Me.InvokeRequired Then
            '*** switch over to primary UI thread
            Dim handler As New UpdateTreeviewHandler(AddressOf UpdateTreeview_Implementation)
            Dim args() As Object = {sFolder}
            Me.BeginInvoke(handler, args)
        Else
            '*** direct call - already running on primary UI thread
            UpdateTreeview_Implementation(sFolder)
        End If
    End Sub

    Private Sub UpdateTreeview_Implementation(ByVal sFolder As String)
        TreeView1.Nodes.Add(sFolder)
        Application.DoEvents()
    End Sub

''''' ***** This added to another class...


Public Class MyThreadClass
    Private delHandler As Form1.UpdateTreeviewHandler
    Public sFolder As String

    Public Sub New(ByVal delHandler As Form1.UpdateTreeviewHandler)
        Me.delHandler = delHandler
    End Sub

    Public Sub Process()
        '
        ' This is the code your thread performs..
        '
        delHandler.BeginInvoke(sFolder, Nothing, Nothing)
    End Sub
End Class
0
Rog DManager Inforamtion SystemsAuthor Commented:
Ok.  Program works fine when not running in IDE...

Now I have added 100 more points to someone who can help me now change the program above to add a node instead of a "String"

Meaning.  In the thread I am building a TreeNode with SubNodes and would like to add the Node with Subnodes to the tree.  I thought this would be easy change from what I asked before and it probably is.  






Thanks,

Rog
0
KGregCommented:
Here you go!


''''' ****  THis was added to the form code as explained above....

 Delegate Sub UpdateTreeviewHandler(ByVal objNode As TreeNode)

    Private Sub UpdateTreeview_Callback(ByVal objNode As TreeNode)
        If Me.InvokeRequired Then
            '*** switch over to primary UI thread
            Dim handler As New UpdateTreeviewHandler(AddressOf UpdateTreeview_Implementation)
            Dim args() As Object = {objNode}
            Me.BeginInvoke(handler, args)
        Else
            '*** direct call - already running on primary UI thread
            UpdateTreeview_Implementation(objNode)
        End If
    End Sub

    Private Sub UpdateTreeview_Implementation(ByVal objNode As TreeNode)
        TreeView1.Nodes.Add(objNode)
        Application.DoEvents()
    End Sub

''''' ***** This added to another class...


Public Class MyThreadClass
    Private delHandler As Form1.UpdateTreeviewHandler
    Public sFolder As String

    Public Sub New(ByVal delHandler As Form1.UpdateTreeviewHandler)
        Me.delHandler = delHandler
    End Sub

    Public Sub Process()
        '
        ' This is the code your thread performs..
        '
        delHandler.BeginInvoke((new System.Windows.Forms.TreeNode("MyTextForTheNode")), Nothing, Nothing)
    End Sub
End Class


Hope this helps!

KGREG
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rog DManager Inforamtion SystemsAuthor Commented:
KGreg,

One quick question...

What I am trying to do is fill several nodes with subnodes, then pass this parent node to the ui to be updated.

If possible what changes would need to be made in your code above to accomidate a node with subnodes rather than the "String" being passed?

Thanks,

Roger
0
KGregCommented:

I'm assuming the goal is to create a node, add a structure of sub-nodes, and pass the parent node to the UI so it can be added.  

All you need to do is build your node structure in the Process() procedure.  When the node has been built, invoke the delegate and pass in the node.  The UI will then add the node to the Nodes collection of the treeview and all will be well in Treeview land.

Here's the code:

''''' ****  THis was added to the form code as explained above....

 Delegate Sub UpdateTreeviewHandler(ByVal objNode As TreeNode)


    '  This is called when the delegate is invoked

    Private Sub UpdateTreeview_Callback(ByVal objNode As TreeNode)
        If Me.InvokeRequired Then
            '*** switch over to primary UI thread
            Dim handler As New UpdateTreeviewHandler(AddressOf UpdateTreeview_Implementation)
            Dim args() As Object = {objNode}
            Me.BeginInvoke(handler, args)
        Else
            '*** direct call - already running on primary UI thread
            UpdateTreeview_Implementation(objNode)
        End If
    End Sub

    '  This is called after the context is set to the UI by our delegate function
    '  It allows the UI to be updated..

    Private Sub UpdateTreeview_Implementation(ByVal objNode As TreeNode)
       
        '
        ' Add the node to the treeview..
        '

        TreeView1.Nodes.Add(objNode)
        Application.DoEvents()
    End Sub

''''' ***** This added to another class...


Public Class MyThreadClass
    Private delHandler As Form1.UpdateTreeviewHandler
    Public sFolder As String

    Public Sub New(ByVal delHandler As Form1.UpdateTreeviewHandler)
        Me.delHandler = delHandler
    End Sub

    Public Sub Process()
        '
        ' Create your node structure..
        '
        dim node as new TreeNode("Parent Node")

        node.Nodes.add(New TreeNode("Child Node")

        '  etc...

        '
        '  Invoke the delegate and pass in the node structure you've created..
        '
        delHandler.BeginInvoke(node, Nothing, Nothing)
    End Sub
End Class

Hope this helps,

KGREG
0
Rog DManager Inforamtion SystemsAuthor Commented:
Thanks...

Works great!
0
KGregCommented:
You're welcome!  I'm glad it worked for you

KGREG
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.