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) .
source share