Why is my C# application running on multi thread apartment?
This is part of my Main method:
[STAThread]
static void Main(string[] args)
{
#if !DEBUG
try
{
#endif
ApartmentState aps = System.Threading.Thread.CurrentThread.GetAp开发者_如何转开发artmentState();
//System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
MessageBox.Show(aps.ToString());
The message shows MTA
. Any idea why or where should I start investigating?
Were you just curious, or was it causing some other issue? Usually it's only a problem if you are trying to do some types of COM interaction in your main method. Windows will still run in the appropriate ApartmentState.
I haven't checked lately, but it used to be that regardless of the attribute, Visual Studio forced MTA. Did you try to run your application from outside of Visual Studio?
If it is only a problem in the debugger, uncheck "Enable the Visual Studio hosting process" under the Debug section of your project's properties.
This attribute sets the COM threading model for an application. If your application isn't using COM interop, it will have no effect.
From MSDN,
COM threading models only pertain to applications that use COM interop. Using this attribute in an application that does not use COM interop has no effect.
精彩评论