Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BackgroundWorker and WebBrowser Control

156 views
Skip to first unread message

James Klett

unread,
Mar 12, 2008, 12:15:57 PM3/12/08
to
System.Threading.ThreadStateException: ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()

So, my app has the WebBrowser control, and I have a background worker.
Apparently in order to use the worker class you must be in MTA mode and NOT
STA mode. But, the WebBrowser control apparently can only be used in STA
mode. Any ideas on how to get around this issue??


thanks,

JIM

Peter Duniho

unread,
Mar 12, 2008, 2:33:01 PM3/12/08
to

I have a suspicion that you are trying to access the WebBrowser class
directly from within your BackgroundWorker. It's not true that "you must
be in MTA mode" to use BackgroundWorker (after all, code that creates a
BackgroundWorker is most often a STA thread), but yes...the background
thread itself may be MTA.

Even if it weren't, you still shouldn't be using the WebBrowser from that
thread though. You should keep all of your GUI objects on the same thread
(the main GUI thread) and you should only access them from that thread.
You do that by using Control.Invoke() or Control.BeginInvoke().

Pete

JPK

unread,
Mar 13, 2008, 2:37:07 PM3/13/08
to
No, the web browser is in the main app thread. If I go STA then when I try
to fire the background worker task, I get the error that the app main thread
cannot be STA. My background worker opens a connection to QuickBooks and
transfers invoices over. The connection to QB is probably what is the
issue. It works all in a single thread, I just wanted to be able to allow
the user to move on to other things while those invoices transfer


Thanks,

JIM


"Peter Duniho" <NpOeS...@nnowslpianmk.com> wrote in message
news:op.t7w1t...@petes-computer.local...

Peter Duniho

unread,
Mar 13, 2008, 3:15:19 PM3/13/08
to
On Thu, 13 Mar 2008 11:37:07 -0700, JPK <ja...@klett.us> wrote:

> No, the web browser is in the main app thread.

Define "in". And how is that relevant to what I wrote?

> If I go STA then when I try
> to fire the background worker task, I get the error that the app main
> thread
> cannot be STA.

That doesn't make any sense at all. The normal state of affairs for a
.NET Forms application is that the main thread _is_ STA. Getting an error
that it can't be doesn't make sense. In what way do you "get the error"?

> My background worker opens a connection to QuickBooks and
> transfers invoices over.
> The connection to QB is probably what is the
> issue.

Why would the connection affect your use of the WebBrowser at all?

Are you using some third-party code that explicitly sets the apartment for
the thread? It surprises me that .NET wouldn't already have set the
apartment for a thread pool thread, but if not I suppose some third-party
code could be interfering. But even so, accessing a control from a
background thread is not kosher. The apartment state may just be a red
herring.

> It works all in a single thread, I just wanted to be able to allow
> the user to move on to other things while those invoices transfer

Well, there's nothing fundamentally impossible about doing something like
that. But you haven't posted a concise-but-complete sample of code that
demonstrates the problem. It's not really possible to provide any
specific advice without such a sample.

Based on the information you've posted so far, I still believe that my
previous reply is relevant. But if you're not finding it helpful, you
need to post more specific information about your question. Create a
concise-but-complete sample of code that reliably demonstrates the issue
and post that. Only then would it be possible for someone to know what
you're doing and what you need to fix.

Pete

JPK

unread,
Mar 13, 2008, 4:02:04 PM3/13/08
to
This is ~ the error I am getting

***
ThreadStateException

Current thread must be set to single apartment(STA) mode before OLE calls
can be made. Ensure that you main function has STA...
***

However, my main function IS STA.

This error fires after executing RunWorkerAsync() which calls the code to
connect to QB

Thanks,

JIM

"Peter Duniho" <NpOeS...@nnowslpianmk.com> wrote in message

news:op.t7yyf...@petes-computer.local...

Peter Duniho

unread,
Mar 13, 2008, 4:27:47 PM3/13/08
to
On Thu, 13 Mar 2008 13:02:04 -0700, JPK <ja...@klett.us> wrote:

> This is ~ the error I am getting
>
> ***
> ThreadStateException
>
> Current thread must be set to single apartment(STA) mode before OLE
> calls can be made. Ensure that you main function has STA...
> ***

That is obviously not the error you're getting verbatim. If you're going
to post an error, it should be the _exact_ text of the error.

That error also does not actaully say "that the app main thread cannot be
STA" as you wrote in your previous message. If anything, it says the
opposite (but it's hard to tell, because the text you wrote is obviously
not the actual error message).

Finally, that tells us _what_ the error says, but it's not an aswer to the
question "in what way" does the error happen?

You also didn't answer any of the other questions I asked, nor did you
post any code at all. As I wrote before, without code there's no way to
offer any more advice than I've already offered.

Pete

Willy Denoyette [MVP]

unread,
Mar 13, 2008, 5:29:29 PM3/13/08
to
"JPK" <ja...@klett.us> wrote in message
news:FF76E37D-0324-44F3...@microsoft.com...

You have posted two different exceptions.
This one from your first post:


System.Threading.ThreadStateException: ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()

and now you say:
***
ThreadStateException

Current thread must be set to single apartment(STA) mode before OLE calls
can be made. Ensure that you main function has STA...
***

The first one was related to your WB control which was created on a non STA
thread, but apparantly you changed your code and now you get something
different.

This exception is thrown on a worker thread, which is initialized by default
as MTA, by a *component* that needs an STA to run. So, aparantly you are
creating/accessing another COM object from a thread that has an
"incompatible apartment" setting as required by the component.

Mind to post the code in RunWorkerAsync?

Willy.


0 new messages