Method Invoking and SyncLock?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Method Invoking and SyncLock?

  1. #1
    Vinny Guest

    Method Invoking and SyncLock?

    Hello!

    My application at the moment is running on various threads. there is a point
    where one of the threads wants to make add a node to a tree view, but it
    wont let me because the thread where i want to change it is not the same as
    the one that created it.

    this all makes sense, i need to use the invoke method. the part that
    confuses me is that i thought the "SyncLock" statement was for...

    could someone kindly explain to me the difference?

    also, i was trying to get it working with the Invoke method. But the problem
    is this: how am i going to add a specific text to the tree view if the
    delegate cannot take any parameters? will i have to make a class level
    variable so that the invoked method and the original method can pass the
    text of the tree node to each other?

    once again, im confused!

    thank you!

    Vinny



  2. #2
    Rob Teixeira Guest

    Re: Method Invoking and SyncLock?


    "Vinny" <vad@stgroup.net> wrote:
    >Hello!
    >
    >My application at the moment is running on various threads. there is a point
    >where one of the threads wants to make add a node to a tree view, but it
    >wont let me because the thread where i want to change it is not the same

    as
    >the one that created it.


    Yep

    >this all makes sense, i need to use the invoke method. the part that
    >confuses me is that i thought the "SyncLock" statement was for...
    >
    >could someone kindly explain to me the difference?


    No. SyncLock is used to sync threads so they don't stomp on the same data
    at the same time. When you create a synclock block, only threads that can
    obtain the lock are allowed to execute and everyone else wanting to get in
    has to wait.

    The problem with multiple threads on WinForms is that the underlying window
    message loop is controlled by the thread that created it. Only that thread
    is allowed to muck with the messages (amoung other things). Since most of
    the methods are really posting and peeking at the messages, this is why those
    methods need to be run from the original thread.

    >also, i was trying to get it working with the Invoke method. But the problem
    >is this: how am i going to add a specific text to the tree view if the
    >delegate cannot take any parameters? will i have to make a class level
    >variable so that the invoked method and the original method can pass the
    >text of the tree node to each other?


    You have to create your own delegate for the node.add() method and pass that
    delegate to the Invoke method.

    Public Delegate Function AddNodeDelegate(ByVal text As String) As TreeNode

    ' then somewhere else in your code:

    Sub DoThreadStuff()
    If TreeView1.InvokeRequired Then
    Dim MyDelegate As AddNodeDelegate = AddressOf TreeView1.Nodes.Add
    TreeView1.Invoke(MyDelegate, New Object() {"Node1 Text"})
    Else
    TreeView1.Nodes.Add("Node1 Text")
    End If
    t.Abort()
    End Sub

    Notice that you pass parameters to the Delegate in the Invoke method by using
    an object array. Also, I added the Abort line to terminate the thread in
    that method. You obviously don't need to do that.


    -Rob

  3. #3
    Vinny Guest

    Re: Method Invoking and SyncLock?

    Hello Rob,

    Yesterday night, as a "work around", i made an object called TreeUpdater.

    what i do in my project is when i want to add a node to the tree view, i add
    that text to a property of the my TreeUpdater object, then i pass in the
    address of a method in the TreeUpdater object, like so:

    Dim Updater as new TreeUpdater(TreeView)
    Updater.Text = "NewNodeText"
    TreeView.Invoke(New MethodInvoker(AddressOf Updater.AddNodeToTree))

    and the AddNodeToTree Method looks like so:

    Friend Sub AddUserToTree()
    m_Tree.Nodes.Add(Me.Text)
    End Sub

    Well, thats my "work around" and it seemed to work just fine. Do you think
    that my "WorkAround" is bad coding? or bad practice? or inefficient?
    compared to your example?

    thank you...

    Vinny



    "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    news:3ca347ba$1@10.1.10.29...
    >
    > "Vinny" <vad@stgroup.net> wrote:
    > >Hello!
    > >
    > >My application at the moment is running on various threads. there is a

    point
    > >where one of the threads wants to make add a node to a tree view, but it
    > >wont let me because the thread where i want to change it is not the same

    > as
    > >the one that created it.

    >
    > Yep
    >
    > >this all makes sense, i need to use the invoke method. the part that
    > >confuses me is that i thought the "SyncLock" statement was for...
    > >
    > >could someone kindly explain to me the difference?

    >
    > No. SyncLock is used to sync threads so they don't stomp on the same data
    > at the same time. When you create a synclock block, only threads that can
    > obtain the lock are allowed to execute and everyone else wanting to get in
    > has to wait.
    >
    > The problem with multiple threads on WinForms is that the underlying

    window
    > message loop is controlled by the thread that created it. Only that thread
    > is allowed to muck with the messages (amoung other things). Since most of
    > the methods are really posting and peeking at the messages, this is why

    those
    > methods need to be run from the original thread.
    >
    > >also, i was trying to get it working with the Invoke method. But the

    problem
    > >is this: how am i going to add a specific text to the tree view if the
    > >delegate cannot take any parameters? will i have to make a class level
    > >variable so that the invoked method and the original method can pass the
    > >text of the tree node to each other?

    >
    > You have to create your own delegate for the node.add() method and pass

    that
    > delegate to the Invoke method.
    >
    > Public Delegate Function AddNodeDelegate(ByVal text As String) As TreeNode
    >
    > ' then somewhere else in your code:
    >
    > Sub DoThreadStuff()
    > If TreeView1.InvokeRequired Then
    > Dim MyDelegate As AddNodeDelegate = AddressOf

    TreeView1.Nodes.Add
    > TreeView1.Invoke(MyDelegate, New Object() {"Node1 Text"})
    > Else
    > TreeView1.Nodes.Add("Node1 Text")
    > End If
    > t.Abort()
    > End Sub
    >
    > Notice that you pass parameters to the Delegate in the Invoke method by

    using
    > an object array. Also, I added the Abort line to terminate the thread in
    > that method. You obviously don't need to do that.
    >
    >
    > -Rob




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center