Setting up Delegates for Threads
-
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!
-
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!
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?
-
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?
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.
-
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.
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.
-
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.
-
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.
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
-
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
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.