Thread in Webbrowser c #

0

I need to use a thread that can work together with a webbrowser in C #. It just needs to run the command, but as it's a visual component I'm not getting it. Below I detail some attempts and examples that I found. If anyone has a light or other idea, please comment !! Thanks

Based on the original article available at link .

      private void runBrowserThread(Uri url)
      {
          var th = new Thread(() => {
              var br = new WebBrowser();
              br.DocumentCompleted += browser_DocumentCompleted;
              br.Navigate(url);
              // aqui ao invés de navegar até a URL, preciso montar meu html
              br.DocumentText =  "<html><body></body></html>"
              Application.Run();
          });

          th.SetApartmentState(ApartmentState.STA);
          th.Start();
      }

      void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
      {
          var br = sender as WebBrowser;
          if (br.Url == e.Url) 
          {
               Console.WriteLine("Natigated to {0}", e.Url);
               Application.ExitThread();   // Stops the thread
          }
      }

I'm having the error

  

You can not use the COM object that has been detached from your underlying RCW. "and threw an exception of type 'System.Runtime.InteropServices.InvalidComObjectException

    
asked by anonymous 20.06.2017 / 05:56

0 answers