Thread in Webbrowser c#

Asked

Viewed 371 times

0

I need to use a thread that can work together with a webbrowser in c#. She just needs to execute the command, but as it is a visual component I’m not getting. Below I detail some attempts and examples I found. If anyone has a light or other idea, please comment!! Thank you

Based on the original article available in https://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread.

      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 mistake

You cannot use the COM object that has been separated from its underlying RCW." e threw an Exception of type 'System.Runtime.Interopservices.Invalidcomobjectexception

  • because it sends the Navigate command, and then will set the HTML in the hand ?

  • 1

    I tested your code and compiled it quietly. I just noticed that on the line: br.DocumentText is missing ; in the end. Are you using windows Forms? WPF?

  • Here also worked smoothly. I just took the line br.DocumentText = "<html><body></body></html>" that besides being missing ; overrides the command navigate and in the event always appears about:blank

  • Thanks for the personal answers. The example above I picked up on the topic I mentioned. As commented instead of navigate I need to mount hmtl. I only let it be as a help. That html is just a test, what I need to run is a little bigger. You can ignore br.Navigate(url);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.