title: Calling method on correct thread when control is created in new Thread()
body: <p>I've created a new <strong>WebBrowser()</strong> control in a new <strong>Thread()</strong>. </p>
  
  <p>The problem I'm having, is that when invoking a delegate for my <strong>WebBrowser</strong> from the <em>Main Thread</em>, the call is occurring on the <em>Main Thread</em>. I would expect this to happen on <strong>browserThread</strong>.</p>
  
  <pre><code>private static WebBrowser defaultApiClient = null;
  
  delegate void DocumentNavigator(string url);
  
  
  private WebApi() {
  
      // Create a new thread responsible 
      // for making API calls.
      Thread browserThread = new Thread(() =&gt; {
  
          defaultApiClient = new WebBrowser();
  
          // Setup our delegates
          documentNavigatorDelegate = new DocumentNavigator(defaultApiClient.Navigate);
  
          // Anonymous event handler
          defaultApiClient.DocumentCompleted += (object sender, WebBrowserDocumentCompletedEventArgs e) =&gt; {
              // Do misc. things
          };
  
          Application.Run();
      });
      browserThread.SetApartmentState(ApartmentState.STA);
      browserThread.Start();
  
  }
  
  DocumentNavigator documentNavigatorDelegate = null;
  private void EnsureInitialized() {
  
      // This always returns "false" for some reason
      if (defaultApiClient.InvokeRequired) {
  
          // If I jump ahead to this call
          // and put a break point on System.Windows.Forms.dll!System.Windows.Forms.WebBrowser.Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders)
          // I find that my call is being done in the "Main Thread".. I would expect this to be done in "browserThread" instead
          object result = defaultApiClient.Invoke(documentNavigatorDelegate, WebApiUrl);
  
      }
  
  }
  </code></pre>
  
  <p>I've tried invoking the method a myriad of ways:</p>
  
  <pre><code>// Calls on Main Thread (as expected)
  defaultApiClient.Navigate(WebApiUrl);
  
  // Calls on Main Thread
  defaultApiClient.Invoke(documentNavigatorDelegate, WebApiUrl); 
  
  // Calls on Main Thread
  defaultApiClient.BeginInvoke(documentNavigatorDelegate, WebApiUrl); 
  
  // Calls on Main Thread
  documentNavigatorDelegate.Invoke(WebApiUrl);
  
  // Calls on random Worker Thread
  documentNavigatorDelegate.BeginInvoke(WebApiUrl, new AsyncCallback((IAsyncResult result) =&gt; { .... }), null);
  </code></pre>
  
  <h2>Update</h2>
  
  <p><em>Let me break down my end-goal a little bit to make things more clear:</em> I have to make calls using <code>WebBrowser.Document.InvokeScript()</code>, however <strong>Document</strong> is not loaded until after I call <code>WebBrowser.Navigate()</code> and THEN the <code>WebBrowser.DocumentComplete</code> event fires. Essentially, I cannot make my intended call to <code>InvokeScript()</code> until after <code>DocumentComplete</code> fires... I would like to WAIT for the document to load (blocking my caller) so I can call <code>InvokeScript</code> and return my result in a synchronous fashion.</p>
  
  <p>Basically I need to wait for my document to complete and the way I would like to do that is with a <code>AutoResetEvent()</code> class which I will trigger upon DocumentComplete being fired... and I need all this stuff to happen in a separate thread.</p>
  
  <p>The other option I see is doing something <em>like</em> this:</p>
  
  <pre><code>private bool initialized = false;
  private void EnsureInitialized(){
      defaultApiClient.Navigate(WebApiUrl);
      while(!initialized){
          Thread.Sleep(1000); // This blocks so technically wouldn't work
      }
  }
  
  private void defaultApiClient_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e){
      initialized = true;
  }
  </code></pre>