473,419 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

ThreadStateException creating ActiveX control in thread

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 Clauss
ca*****@tamu.edu

Nov 16 '05 #1
5 5353
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 [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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 Clauss
ca*****@tamu.edu

Nov 16 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eB***************@TK2MSFTNGP11.phx.gbl...
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 Clauss
ca*****@tamu.edu
Nov 16 '05 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:e2***************@TK2MSFTNGP12.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eB***************@TK2MSFTNGP11.phx.gbl...
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 Clauss
ca*****@tamu.edu

Nov 16 '05 #4
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.

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
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 Clauss
ca*****@tamu.edu

Nov 16 '05 #5
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
ca*****@tamu.edu

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eW*************@TK2MSFTNGP11.phx.gbl...
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Adam Clauss" <ca*****@tamu.edu> wrote in message news:e2***************@TK2MSFTNGP12.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message
news:eB***************@TK2MSFTNGP11.phx.gbl...
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 Clauss
ca*****@tamu.edu



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Baker | last post by:
I have an activeX control which I am calling from a C# program, this works fine. I now want to call the activeX control from a seperate thread, this works but is very slow. This is far too slow...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
5
by: Jaret Brower | last post by:
I'm trying to get Clipboard data from across the network to get a screenshot. I hit a button on the server, it sends a request to the client, when the client get's the request it does a...
3
by: fumihiko | last post by:
Hi, I created an activex control (C++), and it uses another COM dll (C++). This COM dll links with a static library that dose some calculation. (both are debug multithreaded dll) I created a...
0
by: angshuman.agarwal | last post by:
Hi, I have a owner drawn combo box (using .Net 2.0 Framework). I have written the Nunit test for the same. I am setting the AutoCompleteSource Property (AutoCompleteSource.CustomSource) and...
1
by: Pooh | last post by:
hi all, i faced an error in my project: "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in system.windows.forms.dll Additional information: Could not instantiate...
6
by: hufaunder | last post by:
I have an ActiveX component that I want to use in a library that I am writing. As a first test I used the ActiveX component in a windows form application. Adding the component created: Ax.dll...
3
by: cshen | last post by:
Hi All, I have a .net web service try to use an ActiveX control. The code got this error" "cannot be instantiated because the current thread is not in a single-threaded apartment" Someone...
8
by: Daniele Piccinini | last post by:
Hi all, In my vc++ 2005 dialog based application i need to use a comunication activex component in a secondary thread: CFINSAxEFS* pNewAx = new CFINSAxEFS(); if ( !pNewAx->Create( NULL,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.