ThreadStateException creating ActiveX control in thread

  • Thread starter Thread starter Adam Clauss
  • Start date Start date
A

Adam Clauss

I am creating a plugin for MS Outlook. Upon clicking a toolbar button, I create series of threads (variable number each time).
Within each thread, I start a form using Application.Run(...). This form contains the Web Browser ActiveX control. Upon creating
the form, I get a ThreadStateException "Could not instantiate ActiveX control ..... because the current thread is not in a
single-threaded apartment."

Right now, I'm using a ThreadPool to actually create the threads (I just queue up each item). I've searched around and it looks
like I need to set the thread apartment state to STA instead of MTA, but I'm not sure how to actually accomplish that here with the
thread pool.

Any suggestions?

Thanks!
 
Adam,

In order to set the ApartmentState of the current thread, I would do
this:

// Set the apartment state to STA.
Thread.CurrentThread.ApartmentState = ApartmentState.STA;

However, I wouldn't recommned doing this on threads in the thread pool,
because there are a good deal of other operations that depend on the thread
pool, and this could interfere with those.

Rather, I would spawn your own threads, and set the apartment state of
those.

Hope this helps.
 
Nicholas Paldino said:
Adam,

In order to set the ApartmentState of the current thread, I would do
this:

// Set the apartment state to STA.
Thread.CurrentThread.ApartmentState = ApartmentState.STA;

Well, I had already tried adding that line as the first line of the thread function - it had no effect.
Rather, I would spawn your own threads, and set the apartment state of
those.

Hmm, I'll look at doing that, see if that helps any.

Thanks!
 
Adam,

Are you calling it in your thread callback? The call needs to be in
there to take effect.

Also, it is possible that the value had already been set, in which case,
subsequent attempts to set the value are ignored.

You should have no problem if you spawn your own threads.
 
You can't change the apartment state of a thread pool thread, these threads
are MTA threads by default. Use a "regular" thread instead and set it's
state before you call Start.

Willy.
 
OK, it is effectively working now by spawning the threads myself. The only problem I'm having is that I can no longer limit the max
number of threads running at once. A request from the user could generate many threads (conceivably up to about 100, thus I am
testing it that way). With the ThreadPool, I just set the max number of threads to like 15 and it worked great. Now, all 100
threads run at once which results in a serious slowdown.
Any suggestions on how to mimic the cap on the number of threads?
--
Adam Clauss
(e-mail address removed)

Nicholas Paldino said:
Adam,

Are you calling it in your thread callback? The call needs to be in there to take effect.

Also, it is possible that the value had already been set, in which case, subsequent attempts to set the value are ignored.

You should have no problem if you spawn your own threads.
 
Back
Top