Wednesday, March 16, 2011

"Cannot modify the logical children for this node at this time because a tree walk is in progress." Exception

I was doing some tests with localization in a WPF application and I've found that the way to change dynamically the language for a control (and all of its descendants) is to set the Language property. In my application at least, the propagation of change in the entire subtree took a few seconds. As I found out, the way to refresh the language is to actually remove and readd children to control trees. This is probably why simply changing the Thread culture does not work, what the Language property is for and explains why the error was thrown.

During the Language change on the Window object a validation from one of the custom controls there threw a weird Exception: Cannot modify the logical children for this node at this time because a tree walk is in progress.. What could it mean?

This link on StackOverflow says something vague about a bug in the Silverlight charting toolkit, but I think that is wrong, or at least not the root of the problem. I looked through the Reflectored .Net sources and found that this exception is thrown in AddLogicalChild and RemoveLogicalChild methods in FrameworkElement and FrameworkContentElement when the internal property IsLogicalChildrenIterationInProgress is true. Apparently, this property is set to true whenever am internal class DescendentsWalker<T> is "walking" the visual and/or the logical trees. What is this class?

DescendentsWalker<T> seems to be a class used by the WPF framework to find resources, resolve property values, find ancestors/descendants in the control trees, propagate events, etc, so it is a very important low level class. Either because of a bug or exactly in order to prevent one, an error is thrown if someone is trying to change the structure of the tree while something is walking the tree. That is what the error represents.

Now, the only question is how to solve it. The validation code was trying to change the content property of a control, thus modifying the control tree. As such, I do believe this is a slight bug in the framework, as changing the tree could just wait until the tree walk is over, but still. A simple try/catch solve it locally, but what should I do to prevent such errors in the future for all of my code? I can't pad my code with try/catch blocks.

I've tried using Dispatcher.Invoke for the change, it didn't work. The IsLogicalChildrenIterationInProgress property itself does a this.ReadInternalFlag(System.Windows.InternalFlags.IsLogicalChildrenIterationInProgress); and _flags is obviously a private field of the FrameworkElement or FrameworkContentElement class that is never encapsulated into a public property. So I can't check for it in a simple way. Of course, one can always catch Dispatcher.UnhandledException and just set e.Handled to true if this error occurs (Oops, a minor worldwide tremor caused by developer shudder!), but I don't see another global solution.

Well, at least I've thoroughly examined the problem and its causes and possible solutions, however bad or annoying they may be. If you know of a more elegant solution for it, please share it. As this is the only time I ever got this error, I believe it is something that can be handled locally, but only if one knows how to test for it. So, change the Language of your windows or do other lengthy tree walks in order to protect your application from this bug.