Using WebBrowser-Control in multi threaded apps (.Net 2.0)


L

Leon

Hi there,

I am trying to use the WebBrowser Control in a form which is being started
in an own thread by the main form of my application. Unfortunately I am
always getting an error in InitializeComponents stating that the
ActiveX-Control 8856f961-340a-11d0-a96b-00c04fd705a2 cannot be initiated
because the current thread isn't a single-thread apartment.

Is there way of using the webbrowser control in multi threaded
applications in .Net 2.0??

Thanx for your suggestions.
-Leon
 
Ad

Advertisements

M

Marc Gravell

When you say "multi-threaded", are you just talking about your .NET
code? If so, it should be fine to mark your main thread as STA (by
marking your Main() method as [STAThread], but use multiple .NET
threads. The STA/MTA relates to COM calls, not to pure managed code.

If you are genuinely using MTA COM calls then you will need some
different threads; some for the MTA work and some for the STA work.

Marc
 
W

Willy Denoyette [MVP]

Leon said:
Hi there,

I am trying to use the WebBrowser Control in a form which is being started
in an own thread by the main form of my application. Unfortunately I am
always getting an error in InitializeComponents stating that the
ActiveX-Control 8856f961-340a-11d0-a96b-00c04fd705a2 cannot be initiated
because the current thread isn't a single-thread apartment.

Is there way of using the webbrowser control in multi threaded
applications in .Net 2.0??

Thanx for your suggestions.
-Leon


AX Controls are meant to be used from a single STA thread , when used on
multiple threads, you must make sure your thread is initialized to enter an
STA, by calling Thread.SetApartmentState, before you create another thread
that will host the AX control.
Note that I don't see a reason why you need multiple WebBrowser controls in
one single application though.
Willy.
 
L

Leon

This is what I am trying to do:
I have a main form which opens when my application starts and is supposed
to be like the "control" center of my application. Then when i push a
certain button I want to open a new form containing the
WebBrowser-control. As this new form must run in its own thread so that
for example the main form still responds to user input even if the form
with the webbrowser control is blocked.

Maybe using Thread.SetApartmentState will do the trick.
 
W

Willy Denoyette [MVP]

Leon said:
This is what I am trying to do:
I have a main form which opens when my application starts and is supposed
to be like the "control" center of my application. Then when i push a
certain button I want to open a new form containing the
WebBrowser-control. As this new form must run in its own thread so that
for example the main form still responds to user input even if the form
with the webbrowser control is blocked.

Maybe using Thread.SetApartmentState will do the trick.

Note that you have to create the STA thread and you run the new form on this
thread.

Willy.
 
N

Nicholas Paldino [.NET/C# MVP]

While Willy's recommendation will help, the real issue here is that what
you should be doing is hosting the web browser control on separate forms ^in
the same thread^. You can easily create new threads to do work that you
need to do (which I am sure doesn't involve the UI, or rather, can be
crafted to not need the UI) and then use the Invoke method on the
controls/forms to update the UI when needed, keeping all the forms
responsive.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top