From: James Klett on
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

From: Peter Duniho on
On Wed, 12 Mar 2008 09:15:57 -0700, James Klett <james(a)klett.us> wrote:

> 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
From: JPK on
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" <NpOeStPeAdM(a)nnowslpianmk.com> wrote in message
news:op.t7w1tbhi8jd0ej(a)petes-computer.local...
> On Wed, 12 Mar 2008 09:15:57 -0700, James Klett <james(a)klett.us> wrote:
>
>> 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

From: Peter Duniho on
On Thu, 13 Mar 2008 11:37:07 -0700, JPK <james(a)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
From: JPK on
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" <NpOeStPeAdM(a)nnowslpianmk.com> wrote in message
news:op.t7yyftil8jd0ej(a)petes-computer.local...
> On Thu, 13 Mar 2008 11:37:07 -0700, JPK <james(a)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