BackgroundWorker and WebBrowser Control

J

James Klett

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
 
P

Peter Duniho

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??

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
 
J

JPK

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
 
P

Peter Duniho

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
 
J

JPK

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
 
P

Peter Duniho

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
 
W

Willy Denoyette [MVP]

JPK said:
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



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.
 

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