C #: getting queued delegates in a management call list

Suppose I have to take a tree structure and add some nodes to the BFS method , but also in streaming mode.

treeView.Invoke((MethodInvoker)delegate{treeView.Nodes.Add(someNode);}); 

Later, I would like to add node to one of the nodes added to treeView (as my depth increases by one). How do I know that treeView had all the depth added to one node before adding a second layer of nodes. By the way, the information on the second level depends on the first level. All this is just an example of what I need, but it is secondary to the real issue.

Maybe someone is expanding the node, and I would like to add nodes to this node first ... in this case, I would like to abort the treeView call list and start adding nodes to the node that was expanded.

How can I accomplish any of these tasks?

I suppose I could add nodes to the depths of the first mod, but I don't want to. Is there a better way?

I really like the idea of ​​creating a message / delegate queue under the hood, and I cannot control it.

+4
source share
1 answer

How delegates are queuing under the hood

The actual delegates are queued in a private field named threadCallbackList for the control or the nearest parent control that has already been created. The data structure is just System.Collections.Queue .

Knowing that TreeView had all its depth - one node added

If you want to tell the main thread that node had everything that you have children, you can store something (enum) in the node Tag property to indicate this.

If you think you need to go back to the background thread executed by the delegate, which adds the final node to the level of the tree structure, you do not. The delegates you send using BeginInvoke and Invoke are done in order.

Aborting BeginInvoke d delegates

You cannot interrupt the execution of delegated queued delegates in a user interface thread by clicking a control in the same thread. Running a delegate is like any other part of your program. This is a cycle that deserts each delegate and executes them. You are the user interface thread that these delegates will execute so that it cannot also execute the click event handler that would try to abort the delegate queue. Even if you can understand how to use reflection to get confused with the delegates set up, you could not do it at the same time that the other code is running on the same thread.

Possible Solution

It sounds like you're just worried about stroking your user interface stream. If in this case you could throttle the background thread by invoking Invoke instead of BeginInvoke every N th call. This will cause the background thread to wait until the queue is reset (at least until the delegate passed in this call). This way you will never have more than N delegates in line at a time. If you went this route, you could just Invoke every time use AddRange(someNodes) instead of Add(someNode) .

+1
source

All Articles