Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

BackgroundWorker and WebBrowser Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Klett - 12 Mar 2008 17:15 GMT
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 - 12 Mar 2008 19:33 GMT
> System.Threading.ThreadStateException: ActiveX control  
> '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because  
[quoted text clipped - 6 lines]
> 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
JPK - 13 Mar 2008 19:37 GMT
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

>> System.Threading.ThreadStateException: ActiveX control
>> '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because
[quoted text clipped - 19 lines]
>
> Pete
Peter Duniho - 13 Mar 2008 20:15 GMT
> 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 - 13 Mar 2008 21:02 GMT
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

>> No, the web browser is in the main app thread.
>
[quoted text clipped - 39 lines]
>
> Pete
Peter Duniho - 13 Mar 2008 21:27 GMT
> This is ~ the error I am getting
>
[quoted text clipped - 4 lines]
> 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] - 13 Mar 2008 22:29 GMT
> This is ~ the error I am getting
>
[quoted text clipped - 58 lines]
>>
>> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.