title: Using Caching with AutomationElements does not give me a speedup - intended or wrong usage?
body: <p>I am trying to increase the speed of some UI Automation operations. I stumbled across the (not so well) documented caching possibility.</p>
  
  <p>From what I have understood, the whole operation (if you have a large GUI-tree) is so slow, because for every single function call there has to be a process-change (kind of like going to kernel-mode, I suppose, speed-wise?!). So.. in comes caching.</p>
  
  <p>Simply tell the function to cache an element and its children, and then work on it lightning-fast. (From what I understand, you only have one context-change, and assemble all the data you need in one go.)</p>
  
  <p>Good idea, but.. it is just as slow for me as the uncached variation. I wrote some simple testcode, and could not see an improvement.</p>
  
  <pre><code>AutomationElement ae; // element whose siblings are to be examined, thre are quite a few siblings
  AutomationElement sibling;
  
  #region non-cached
  watch.Start();
  for (int i = 0; i &lt; 10; ++i)
  {
      sibling = TreeWalker.RawViewWalker.GetFirstChild(TreeWalker.RawViewWalker.GetParent(ae));
      while (sibling != null)
      {
          sibling = TreeWalker.RawViewWalker.GetNextSibling(sibling);
      }
  }
  watch.Stop();
  System.Diagnostics.Debug.WriteLine("Execution time without cache: " + watch.ElapsedMilliseconds + " ms.");
  #endregion
  
  #region cached
  watch.Reset();
  watch.Start();
  
  CacheRequest cacheRequest = new CacheRequest();
  cacheRequest.TreeScope = TreeScope.Children | TreeScope.Element; // for testing I chose a minimal set
  AutomationElement parent;
  
  for (int j = 0; j &lt; 10; ++j)
  {
      using (cacheRequest.Activate())
      {
          parent = TreeWalker.RawViewWalker.GetParent(ae, cacheRequest);
      }
      int cnt = parent.CachedChildren.Count;
      for (int i = 0; i &lt; cnt; ++i)
      {
          sibling = parent.CachedChildren[i];
      }
  }
  watch.Stop();
  System.Diagnostics.Debug.WriteLine("Execution time parentcache: " + watch.ElapsedMilliseconds + " ms.");
  #endregion
  </code></pre>
  
  <p>The setup is: you get an element and want to check on all its (many) siblings. Both implementations, without and with cache, are given.</p>
  
  <p>The output (debug mode):
  Execution time without cache: 1130 ms.
  Execution time parentcache: 1271 ms.</p>
  
  <p>Why is this not working? How to improve?</p>
  
  <p>Thanks for any ideas!!!</p>