Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Setting up Delegates for Threads

Setting up Delegates for Threads

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    treddie
    wrote on last edited by
    #1

    Hi again. I am a little confused about setting up delegates. I believe this is correct: If I have a number of individual statements such as:

    MyLocalTreeView.TreeView1.SelectedNode.Parent.FullPath
    MyLocalTreeView.TreeView1.SelectedNode.FullPath
    MyLocalTreeView.TreeView1.SelectedNode.Expand()
    Me.MyLocalTreeView.TreeView1.Focus()

    am I correct in assuming that separate delegates for each must be built, if they do not all happen together in some recurring usage where they would all be lumped together in one sub? Thanks for your help!

    R 1 Reply Last reply
    0
    • T treddie

      Hi again. I am a little confused about setting up delegates. I believe this is correct: If I have a number of individual statements such as:

      MyLocalTreeView.TreeView1.SelectedNode.Parent.FullPath
      MyLocalTreeView.TreeView1.SelectedNode.FullPath
      MyLocalTreeView.TreeView1.SelectedNode.Expand()
      Me.MyLocalTreeView.TreeView1.Focus()

      am I correct in assuming that separate delegates for each must be built, if they do not all happen together in some recurring usage where they would all be lumped together in one sub? Thanks for your help!

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      Are you using the term "delegate" correctly? Delegates are basically function pointers that can be passed around like variables. In VB.NET delegates are created using the AddressOf operator. See this: Delegates in VB.NET[^] Maybe I'm not getting what you are trying to do? Are you talking about a subroutine to do all the above operations in one call?

      T 1 Reply Last reply
      0
      • R Ron Beyer

        Are you using the term "delegate" correctly? Delegates are basically function pointers that can be passed around like variables. In VB.NET delegates are created using the AddressOf operator. See this: Delegates in VB.NET[^] Maybe I'm not getting what you are trying to do? Are you talking about a subroutine to do all the above operations in one call?

        T Offline
        T Offline
        treddie
        wrote on last edited by
        #3

        Hi, Ron. Thanks for your response. I am referring to the fact that a form's controls cannot be directly accessed via a thread other than the main thread that runs the form itself. Delegates are required. That being the case, if I have a TreeView that must be updated from a second thread, do I need to create separate delegates for each and every method I will use for the TreeView? It seems that unless I always use multiple TreeView methods in a set order, and put all of that into a sub that will have a delegate assigned to it, then I will need to do them all separately. Hm...just thought of this...Unless it is possible to create a delegate for the entire TreeView class.

        R G 2 Replies Last reply
        0
        • T treddie

          Hi, Ron. Thanks for your response. I am referring to the fact that a form's controls cannot be directly accessed via a thread other than the main thread that runs the form itself. Delegates are required. That being the case, if I have a TreeView that must be updated from a second thread, do I need to create separate delegates for each and every method I will use for the TreeView? It seems that unless I always use multiple TreeView methods in a set order, and put all of that into a sub that will have a delegate assigned to it, then I will need to do them all separately. Hm...just thought of this...Unless it is possible to create a delegate for the entire TreeView class.

          R Offline
          R Offline
          Ron Beyer
          wrote on last edited by
          #4

          Yeah, "delegate" is not the right term. Operations on UI items can only be performed on the thread that created them, typically (almost always) the UI thread. When you want to do something, you have to Invoke on the UI thread. Don't confuse the word invoke with delegates, a delegate can be invoked, but its really not the same concept. You can create a delegate to invoke on the UI, but there are other options. The Control.Invoke method invokes on the UI thread, you don't have to use the TreeView.Invoke for just treeview items, you can update any UI item in that invoke call since its running in the UI thread. You can even run complicated sets of code in there if you want that have nothing to do with the UI.

          T 1 Reply Last reply
          0
          • R Ron Beyer

            Yeah, "delegate" is not the right term. Operations on UI items can only be performed on the thread that created them, typically (almost always) the UI thread. When you want to do something, you have to Invoke on the UI thread. Don't confuse the word invoke with delegates, a delegate can be invoked, but its really not the same concept. You can create a delegate to invoke on the UI, but there are other options. The Control.Invoke method invokes on the UI thread, you don't have to use the TreeView.Invoke for just treeview items, you can update any UI item in that invoke call since its running in the UI thread. You can even run complicated sets of code in there if you want that have nothing to do with the UI.

            T Offline
            T Offline
            treddie
            wrote on last edited by
            #5

            Ah, cool. Thanks for the clarification. I think you just made my day in getting my mind around delegates and Invoke, and how they relate.

            1 Reply Last reply
            0
            • T treddie

              Hi, Ron. Thanks for your response. I am referring to the fact that a form's controls cannot be directly accessed via a thread other than the main thread that runs the form itself. Delegates are required. That being the case, if I have a TreeView that must be updated from a second thread, do I need to create separate delegates for each and every method I will use for the TreeView? It seems that unless I always use multiple TreeView methods in a set order, and put all of that into a sub that will have a delegate assigned to it, then I will need to do them all separately. Hm...just thought of this...Unless it is possible to create a delegate for the entire TreeView class.

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              This may be of help for referencing items on a separate thread: Create an extension method to invoke items:

              Module Extensions

               \_
              Sub SynchronisedInvoke(synchMe As ISynchronizeInvoke, action As Action)
              
                  If Not synchMe.InvokeRequired Then
              
                      action()
              
                  Else
              
                      synchMe.Invoke(action, New Object() {})
              
                  End If
              
              End Sub
              

              End Module

              Then you can reference the item on the other thread with the use of a lambda:

              lblMessage.SynchronisedInvoke(Sub() lblMessage.Text = "Hello World")

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              T 1 Reply Last reply
              0
              • G GuyThiebaut

                This may be of help for referencing items on a separate thread: Create an extension method to invoke items:

                Module Extensions

                 \_
                Sub SynchronisedInvoke(synchMe As ISynchronizeInvoke, action As Action)
                
                    If Not synchMe.InvokeRequired Then
                
                        action()
                
                    Else
                
                        synchMe.Invoke(action, New Object() {})
                
                    End If
                
                End Sub
                

                End Module

                Then you can reference the item on the other thread with the use of a lambda:

                lblMessage.SynchronisedInvoke(Sub() lblMessage.Text = "Hello World")

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                T Offline
                T Offline
                treddie
                wrote on last edited by
                #7

                Sorry for responding late. My A/C went out last night, and I need to get a window unit in for temporary use. In the meantime, it is 91degF inside my home and I can only use my laptop for brief stints, that with a cooler pad underneath. My desktop computer needs to stay shut down.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups