From: Peter Duniho on
On Thu, 13 Mar 2008 13:02:04 -0700, JPK <james(a)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
From: Willy Denoyette [MVP] on
"JPK" <james(a)klett.us> wrote in message
news:FF76E37D-0324-44F3-902E-B4925F47F728(a)microsoft.com...
> 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
>



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.