Questions tagged [apartment-state]
20 questions
27
votes
2 answers
How to check apartment state of current thread?
I have a function which requires to be run in STA apartment state. I wan't to check if it is being run as STA, and if not spawn a new thread which runs in STA.
How can I check which apartment state the current thread is being run in?

stiank81
- 25,418
- 43
- 131
- 202
10
votes
2 answers
NUnit GUI Runner and Apartment State
How do you set the apartment state in the NUnit GUI runner? I'm trying to run a single NUnit test with WatiN and I'm getting the message:
MyNamespace.LoginTests.CanLogin:
System.Threading.ThreadStateException : The CurrentThread needs to have…

BuddyJoe
- 69,735
- 114
- 291
- 466
7
votes
4 answers
How many 'STA' threads can coexist inside a process?
I need multiple STA threads inside my ASP.NET application to use some COM components.
I read somewhere inside each process, only one STA thread can exist. I coded a sample project and made many threads and set their apartment state to STA and they…

Xaqron
- 29,931
- 42
- 140
- 205
3
votes
1 answer
ASP.Net WebApi STA Mode
This post scratches off WebApi from being able to run in STA mode. But this post is also 2 years old.
I've been digging into the newer WebApi2 but can't seem to find a way to make MessageHandlers run in STA mode.
So has anyone found a way of running…

Mrchief
- 75,126
- 20
- 142
- 189
3
votes
1 answer
How to Query the COM Apartment State for the Current Thread
The .NET Thread class has a handy function GetApartmentState() that returns the apartment of context of the thread. Is there a similar Co function for native code (C++, C)? I feel like I should know, since I've been programming…

Max
- 170
- 1
- 10
3
votes
1 answer
ActiveX control cannot be instantiated because thread is in a single-threaded apartment. Happens after waking PC from sleep
I am working on a VOIP client (Window's form) that opens a browser window (ExtendedWebBrowser) when a call is made or received. Normally it works fine but if the computer is put into sleep mode and then woken the program will crash with the…

madeFromCode
- 721
- 7
- 15
2
votes
1 answer
How to get the main thread of a console app to be MTA?
With Delphi, how can I create a (Windows) console application that has a main thread that is initialized as COINIT_MULTITHREADED?
If I call CoInitializeEx(nil, COINIT_MULTITHREADED) in the very first statement, I get a HRESULT 0x80010106 (Cannot…

Martin
- 1,986
- 15
- 32
2
votes
1 answer
Winforms: One COM object needs an STAThread, the other needs an MTAThread. How can I use them?
I'm trying to build a Winforms application with two COM components. However, one of the components only works when using [MTAThread] and the other only works with [STAThread].
What would the recommended solution be?

hb.
- 1,705
- 5
- 22
- 43
1
vote
1 answer
STAThread requirement for windows forms
AFAIK, all Windows Forms application should set the [STAThread] directive above their Main() method.
However, there's an exception. We are writing applications for Windows Mobile devices, using the Compact Framework. Compact framework doesn't even…

bvdb
- 22,839
- 10
- 110
- 123
1
vote
1 answer
Parallel Foreach throw an ApartmentState exception
I have the following code for creating tabpages to a predefined list:
private void CreateControls()
{
Parallel.ForEach(_websites,
w =>
{
var tabPage = new TabPage(w.Name);
var…

ehh
- 3,412
- 7
- 43
- 91
1
vote
3 answers
C# Shutdown A Thread That Uses Application.Run()
I'm trying to use a C# DLL that I have access to the code of, but I can't actually change the code and build a custom version of it. The calling code is a C# WinForms project and the C# DLL also uses WinForms to handle Windows system events. The…

Chris Graham
- 125
- 2
- 7
1
vote
0 answers
Cannot set current thread apartment to STA in C# Windows application main program
I've created a small Windows Application project in Visual Studio 2010, and I cannot set the apartment property of the main thread to STA, it is set to MTA by default, even with the property [STAThread] or [STAThreadAttribute] on my main…

JulienVan
- 881
- 1
- 11
- 30
1
vote
2 answers
TeamCity SpecFlow NUnit Watin and ApartmentState STA
Im trying to get our specflow watin tests to run on our new teamcity server. The problem is that I cant get the built in nunit runner to run tests in appartmentstate STA.
We used this configuration earlier, which works with other…

MatteS
- 1,542
- 1
- 16
- 35
0
votes
1 answer
can I modify the nature of the thread that will BeginInvoke my delegate?
I am implementing an interface that has a BeginSomething() and EndSomething() pair, and my implementation is in a method Execute()
So I create an Action action = Execute, and call action.BeginInvoke in BeginSomething, and action.EndInvoke in…

Louis Rhys
- 34,517
- 56
- 153
- 221
0
votes
1 answer
When creating a Thread on a class called from an STAThread instance, what will the apartment state be?
I have project A and project B. Project B has a thread that does some work with Microsoft Outlook Interop. Project A is a GUI and is marked as STAThread on the main entry point.
If I call project b from within A, will the thread created in project B…

Kai Day
- 21
- 5