JulienVan
JulienVan

Reputation: 891

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 method. This leads to raising exceptions when I display a form and change its controls properties.

Here is a screenshot of my application entry point where I display the value of the main thread ApartmentSate property and try to change it to STA (I don't do this in my application, it's just to show what happens in case someone would suggest to change it pro grammatically):

The ApartmentState property of the main thread is set to MTA

Below is a screenshot of my application properties in the Debug section:

Debug properties of my application

I don't understand because I don't leave the main thread. Would you know what can be the origin of this issue and how I can fix it?

EDIT: If I disable the option 'Enable the Visual Studio hosting process', the ApartmentSate property is correctly set to STA. Is it the best way to avoid the issue, or can I fix this with a better solution?

Thank you

Upvotes: 1

Views: 1481

Answers (0)

Related Questions