1

I have 2 methods in a class (login() and change()).

In that class I also have a property (webBrownser wb)

I call the login() by thread

Thread tLogin = new Thread(b.doLogin);
tLogin.SetApartmentState(ApartmentState.STA);
tLogin.Start();

and the method change() by thread too

 Thread tLike = new Thread(b.autoLike);
 tLike.SetApartmentState(ApartmentState.STA);
 tLike.Start();

But when the method change() try use property wb I receive this message:

COM object that has been separated from its underlying RCW cannot be used.

What am I doing wrong?

The methods are synchronized with lock()...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1691243
  • 21
  • 1
  • 3
  • You will have to create a new instance of the browser. The old one is dead after the thread that owns it terminated. – Hans Passant Sep 22 '12 at 17:39

1 Answers1

0

A WebBrowser is a COM (or ActiveX) component. It can't be used on any other thread than the one that created with object. And that thread needs to have a message pump (e.g. Application.Run in WinForms).

For more detail on how to do this, see WebBrowser Control in a new thread

Community
  • 1
  • 1
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • Yes, anything you do to `WebBrowser` on a thread needs to be done on the same thread that created it and that thread needs to have a message pump. – Peter Ritchie Sep 22 '12 at 17:50