/// <summary>
        /// Get IUIAutomationTreeWalker based on inidcated mode.
        /// </summary>
        /// <param name="mode">TreeViewMode to get walker</param>
        /// <returns></returns>
        public static IUIAutomationTreeWalker GetTreeWalker(TreeViewMode mode)
        {
            IUIAutomationTreeWalker walker = null;

            CUIAutomation h = A11yAutomation.GetUIAutomationObject();

            switch (mode)
            {
            case TreeViewMode.Content:
                walker = h.ContentViewWalker;
                break;

            case TreeViewMode.Control:
                walker = h.ControlViewWalker;
                break;

            case TreeViewMode.Raw:
                walker = h.RawViewWalker;
                break;
            }

            return(walker);
        }
        /// <summary>
        /// Refresh the property and patterns with Live data.
        /// it also set the Glimpse
        /// the update is done via caching to improve performance.
        /// </summary>
        /// <param name="useProperties">default is false to refresh it from UIElement directly</param>
        private static void PopulatePropertiesAndPatternsFromCache(this A11yElement element)
        {
            try
            {
                // Get the list of properties to retrieve
                A11yAutomation.GetUIAutomationObject().PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns);

                var ppl = new List <Tuple <int, string> >();

                for (int i = 0; i < ppids.Length; i++)
                {
                    var id   = (int)ppids.GetValue(i);
                    var name = (string)ppns.GetValue(i);

                    if (DesktopElement.IsExcludedProperty(id, name) == false)
                    {
                        ppl.Add(new Tuple <int, string>(id, name));
                    }
                }

                A11yAutomation.GetUIAutomationObject().PollForPotentialSupportedPatterns((IUIAutomationElement)element.PlatformObject, out int[] ptids, out string[] ptns);
                var ptl = new List <Tuple <int, string> >();

                for (int i = 0; i < ptids.Length; i++)
                {
                    ptl.Add(new Tuple <int, string>((int)ptids.GetValue(i), (string)ptns.GetValue(i)));
                }

                var pplist = (from pp in ppl
                              select pp.Item1).ToList();
                var ptlist = (from pt in ptl
                              select pt.Item1).ToList();

                // build a cache based on the lists
                var cache = DesktopElementHelper.BuildCacheRequest(pplist, ptlist);

                // buildupdate cached element
                var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache);

                // retrieve properties from cache
                var ps = (from pp in ppl//.AsParallel()
                          let val = GetPropertyValueFromCache(uia, pp.Item1)
                                    where val != null
                                    select new A11yProperty(pp.Item1, val, pp.Item2));

                element.Properties = ps.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id);

                element.UpdateGlimpse();

                // retrieve patterns from cache
                var ptlst = from pt in ptl
                            let pi = A11yPatternFactory.GetPatternInstance(element, uia, pt.Item1, pt.Item2)
                                     where pi != null
                                     select pi;

                element.Patterns = ptlst.ToList();

                // release previous UIAElement
                Marshal.ReleaseComObject(uia);
                // release cache interface.
                Marshal.ReleaseComObject(cache);

                ppl?.Clear();
                ptl?.Clear();
            }
            catch
            {
            }
        }
 /// <summary>
 /// Build a cacherequest for properties and patterns
 /// </summary>
 /// <param name="pps">Property ids</param>
 /// <param name="pts">Pattern ids</param>
 public static IUIAutomationCacheRequest BuildCacheRequest(List <int> pps, List <int> pts)
 {
     return(GetPropertiesCache(A11yAutomation.GetUIAutomationObject(), pps, pts));
 }