title: How should i properly invoke a WebBrowser using multiplethreads?
body: <p><strong>Problem Scope:</strong></p>
  
  <p>I'm writing an aplication to save the HTML's retrieved from the <a href="http://www.bing.com" rel="nofollow">Bing</a> and <a href="http://www.google.com" rel="nofollow">Google</a> searches. I know there are classes to execute the Web Requests using stream such as <a href="http://msdn.microsoft.com/en-us/library/456dfw4f.aspx" rel="nofollow">this example</a>, but since Google and Bing both use Javascript and Ajax to render the results into the HTML, there's no way i can simply read the stream and use get to the result i need.</p>
  
  <p>The solution to this, is to use the WebBrowser class and navigate to the url i want, so that the Browser itself will handle all the Javascript and Ajax scripting executions.</p>
  
  <p><strong>MultiThreading:</strong></p>
  
  <p>In order to make it more efficient, i have the same Form aplication firing a thread for each service (one for Bing, and one for Google). </p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Since i need the WebBrowser, i have instantiated one for each thread (which are 2, at this moment). According to Microsoft, there is a <code>known bug that prevents the DocumentCompleted event from firing if the WebBrowser is not visible and is not added to a visible form aswell</code> (for more information, <a href="http://support.microsoft.com/kb/259935" rel="nofollow">follow this link</a>).</p>
  
  <p><strong>Real Problem:</strong></p>
  
  <p>The main issue is that, the <code>DocumentCompleted</code> event of the browser, never fires. Never.</p>
  
  <p>I have wrote a proper handler for the <code>DocumentCompleted</code> event that never gets the callback. For handling the wait needed for the Browser event to fire, i have implemented a <a href="http://msdn.microsoft.com/pt-br/library/system.threading.autoresetevent.aspx" rel="nofollow">AutoResetEvent</a> with a high timeout (5 minutes), that will dispose the webbrowser thread if it does not fire the event i need after 5 minutes.</p>
  
  <p>At the moment, i have the Browser created and added into a WindowsForm, both are visible, and the event is still not firing.</p>
  
  <p><strong>Some Code:</strong></p>
  
  <pre><code>        // Creating Browser Instance
          browser = new WebBrowser ();
  
          // Setting up Custom Handler to "Document Completed" Event
          browser.DocumentCompleted += DocumentCompletedEvent;
  
          // Setting Up Random Form
          genericForm = new Form();
          genericForm.Width = 200;
          genericForm.Height = 200;
          genericForm.Controls.Add (browser);
          browser.Visible = true;  
  </code></pre>
  
  <p>As for the Navigation i have the Following (method for the browser) :</p>
  
  <pre><code>    public void NavigateTo (string url)
      {
          CompletedNavigation = false;
  
          if (browser.ReadyState == WebBrowserReadyState.Loading) return;
  
          genericForm.Show (); // Shows the form so that it is visible at the time the browser navigates
          browser.Navigate (url);
      }
  </code></pre>
  
  <p>And, for the call of the Navigation i have this : </p>
  
  <pre><code>            // Loading URL
              browser.NavigateTo(URL);
  
              // Waiting for Our Event To Fire
              if (_event.WaitOne (_timeout))
              {
                 // Success
              }
              { // Error / Timeout From the AutoResetEvent } 
  </code></pre>
  
  <p><strong>TL:DR:</strong></p>
  
  <p>My WebBrowser is instantiated into a another STAThread, added to a form, both are visible and shown when the Browser Navigation fires, but the DocumentCompleted event from the Browser is never fired, so the AutoResetEvent always times out and i have no response from the browser.</p>
  
  <p>Thanks in Advance and sorry for the long post</p>