title: WebBrowser Control in a web application
body: <p>I tried to use the WebBrowser control in an ASP .NET application: </p>
  
  <pre><code>public BrowserForm()
          {
              webBrowser1 = new WebBrowser();
              webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
          }
  private void webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
      {
     // code here
      }
  </code></pre>
  
  <p>But got error:</p>
  
  <blockquote>
    <p>'8856f961-340a-11d0-a96b-00c04fd705a2'
    cannot be instantiated because the
    current thread is not in a
    single-threaded apartment</p>
  </blockquote>
  
  <p>Then I did something like this:</p>
  
  <pre><code>     public BrowserForm()
          {
              ThreadStart ts = new ThreadStart(StartThread);
              var t = new Thread(ts);
              t.SetApartmentState(ApartmentState.STA);
              t.Start();
  
          }
          [STAThread]
          public void StartThread()
          {
              webBrowser1 = new WebBrowser();
              webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
          }
  
          [STAThread]
   private void webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
          {
             //code here
          }
  </code></pre>
  
  <p>But still it's not working for me as desired...giving me weired errors like:</p>
  
  <blockquote>
    <p>Error HRESULT E_FAIL has been returned
    from a call to a COM component</p>
  </blockquote>
  
  <p>Any work around?? I'm not an expert of threading or COM but trying to convert a WindowApplication to WebApplication which takes a screenshot of a web page provided a URL. :(</p>