By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,950 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,950 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Oct 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Oct 31 '07 #2

P: n/a
"Leon" <menospam@DOya_NOThoo_SPAM.dewrote in message
news:O1*************@TK2MSFTNGP05.phx.gbl...
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.

Oct 31 '07 #3

P: n/a
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.

Willy Denoyette [MVP] wrote:
"Leon" <menospam@DOya_NOThoo_SPAM.dewrote in message
news:O1*************@TK2MSFTNGP05.phx.gbl...
>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.
Oct 31 '07 #4

P: n/a
"Leon" <bcmail08-news@DOya_NOThoo_SPAM.dewrote in message
news:OC****************@TK2MSFTNGP05.phx.gbl...
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.

Oct 31 '07 #5

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Leon" <bcmail08-news@DOya_NOThoo_SPAM.dewrote in message
news:OC****************@TK2MSFTNGP05.phx.gbl...
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.

Willy Denoyette [MVP] wrote:
>"Leon" <menospam@DOya_NOThoo_SPAM.dewrote in message
news:O1*************@TK2MSFTNGP05.phx.gbl...
>>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.

Oct 31 '07 #6

P: n/a
Thanks for your help. SetApartmentState was just what I was looking for :-)

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Nov 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.