From: sheperson on
Hi,
I am trying to use a Web Browser control in my program. My program is
not a single thread, and I really need to use threading. When I create
a Web Browser object in the child thread, I get this error:
---
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---
I know this is because of the threading issue.

Is there a work around to this problem? Any ideas, solutions?
Thanks in advance.
From: sheperson on
On Jun 20, 10:33 pm, Peter Duniho <NpOeStPe...(a)NnOwSlPiAnMk.com>
wrote:
> sheper...(a)googlemail.com wrote:
> > Hi,
> > I am trying to use a Web Browser control in my program. My program is
> > not a single thread, and I really need to use threading. When I create
> > a Web Browser object in the child thread, I get this error:
> > ---
> > ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be
> > instantiated because the current thread is not in a single-threaded
> > apartment.
> > ---
> > I know this is because of the threading issue.
>
> > Is there a work around to this problem? Any ideas, solutions?
> > Thanks in advance.
>
> The error tells you exactly what the problem is.
>
> Create the control in a thread that _is_ a single-threaded apartment thread.
>
> If you're trying to use the web browser control, then you ought to have
> an STA thread already.  Use that thread to create and use the control.
>
> If you don't have an STA thread already, then you have a lot more work
> to do before you can host the web browser control.
>
> Pete

Thanks for your answer.

Here is what I am trying to do:
I have a main thread, in which I get some links. I should navigate
these links one by one, and store their data.
They must be visited one by one, in order; so I have to wait for the
process to finish.

I try to make another thread and navigate to it, using that thread and
join it until it finishes, but when I create a Web Browser control, I
get that message.
From: Peter Duniho on
sheperson(a)googlemail.com wrote:
> Here is what I am trying to do:
> I have a main thread, in which I get some links. I should navigate
> these links one by one, and store their data.
> They must be visited one by one, in order; so I have to wait for the
> process to finish.
>
> I try to make another thread and navigate to it, using that thread and
> join it until it finishes, but when I create a Web Browser control, I
> get that message.

Is this a GUI program? Is your main thread an STA thread?

If so, then create the web browser control on that thread. The
navigation should not have to be synchronous, so it shouldn't be a
problem to start create the control and start the navigation from the
main thread.

If this is not a GUI program, then it's not clear to me why you are
using the ActiveX web browser control in the first place. If all you
want is the raw data from an HTTP server, you can use HttpWebRequest to
get that.

Pete
From: sheperson on
On Jun 21, 8:09 am, Peter Duniho <NpOeStPe...(a)NnOwSlPiAnMk.com> wrote:
> sheper...(a)googlemail.com wrote:
> > Here is what I am trying to do:
> > I have a main thread, in which I get some links. I should navigate
> > these links one by one, and store their data.
> > They must be visited one by one, in order; so I have to wait for the
> > process to finish.
>
> > I try to make another thread and navigate to it, using that thread and
> > join it until it finishes, but when I create a Web Browser control, I
> > get that message.
>
> Is this a GUI program?  Is your main thread an STA thread?
>
> If so, then create the web browser control on that thread.  The
> navigation should not have to be synchronous, so it shouldn't be a
> problem to start create the control and start the navigation from the
> main thread.
>
> If this is not a GUI program, then it's not clear to me why you are
> using the ActiveX web browser control in the first place.  If all you
> want is the raw data from an HTTP server, you can use HttpWebRequest to
> get that.
>
> Pete

Thanks for your reply Pete.
I solved it!
It was a GUI application, and I solved the issue by creating the
WebBrowser in the main thread.
Thanks again for your help. :D
 | 
Pages: 1
Prev: newsgroups being disbanded ?
Next: SoundPlayer