private int PopulateChildrenTreeNode()

in src/Desktop/UIAutomation/TreeWalkers/TreeWalkerForTest.cs [114:168]


        private int PopulateChildrenTreeNode(A11yElement rootNode, A11yElement parentNode, int startChildId)
        {
            this.Elements.Add(rootNode);

            rootNode.Parent = parentNode;
            rootNode.TreeWalkerMode = this.WalkerMode; // set tree walker mode.

            IUIAutomationTreeWalker walker = A11yAutomation.GetTreeWalker(this.WalkerMode);
            IUIAutomationElement child = (IUIAutomationElement)rootNode.PlatformObject;

            if (child != null)
            {
                try
                {
                    child = walker.GetFirstChildElement(child);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
                {
                    ex.ReportException();
                    child = null;
                    System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex);
                }
#pragma warning restore CA1031 // Do not catch general exception types

                while (child != null && _elementCounter.TryIncrement())
                {
#pragma warning disable CA2000 // childNode will be disposed by the parent node
                    // Create child without populating basic property. it will be set all at once in parallel.
                    var childNode = new DesktopElement(child, true, false);
#pragma warning restore CA2000 // childNode will be disposed by the parent node

                    rootNode.Children.Add(childNode);
                    childNode.Parent = rootNode;
                    childNode.UniqueId = startChildId++;
                    startChildId = PopulateChildrenTreeNode(childNode, rootNode,startChildId);
                    try
                    {
                        child = walker.GetNextSiblingElement(child);
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch (Exception ex)
                    {
                        ex.ReportException();
                        child = null;
                        System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex);
                    }
#pragma warning restore CA1031 // Do not catch general exception types
                }
            }

            Marshal.ReleaseComObject(walker);

            return startChildId;
        }