title: DrawHighlight() in Coded UI can not work in C# Console Application
body: <p>I'm trying to make an automation script (not for testing purpose) using Coded UI, that works in Console Application.</p>
  
  <p>This webpage helped me quite much. 
  <a href="http://blogs.microsoft.co.il/shair/2010/07/15/running-codedui-test-from-another-application/" rel="nofollow noreferrer">http://blogs.microsoft.co.il/shair/2010/07/15/running-codedui-test-from-another-application/</a></p>
  
  <p>However, if I use DrawHighlight(), I will get below exception</p>
  
  <p>System.InvalidOperationException:{"The calling thread must be STA, because many UI components require this."}</p>
  
  <p>I did some research on this exception, and all the solutions are for WPF or winform app, and I can not make it work for me.</p>
  
  <p>My program is below:</p>
  
  <pre><code>using Microsoft.VisualStudio.TestTools.UITesting;
  using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
              Playback.PlaybackSettings.SearchTimeout = 10000;
              Playback.Initialize();
              var browser = BrowserWindow.Launch("https://www.google.com/");
              HtmlEdit uIGoogleEdit = new HtmlEdit(browser);
              uIGoogleEdit.SearchProperties.Add(HtmlControl.PropertyNames.Name, "q");
              uIGoogleEdit.DrawHighlight();
              //......(do something here)......
              Playback.Cleanup();
          }
      }
  }
  </code></pre>
  
  <p>Below is a piece of code that I ever tried, but not working(replacing "uIGoogleEdit.DrawHighlight()" in above):</p>
  
  <pre><code>        Thread myThread = new Thread(new ThreadStart(delegate ()
          {
                  Dispatcher.CurrentDispatcher.Invoke(
                      DispatcherPriority.Normal,
                      new Action(delegate ()
                      {
                          uIGoogleEdit.DrawHighlight();
                      }));
          }));
  
          myThread.SetApartmentState(ApartmentState.STA);
          myThread.Start();
  </code></pre>