Greetings.
I'm developing a WinForms-driven application, and I need it to use Single Threaded model for COM interop.
C# 2003 worked fine with [STAThread] attribute above main() declaration. But we just upgraded our machines to VS 2005, and our application is failing due COM errors. We found C# 2005 is creating the main thread as Multithreading model, regardless the STA attribute.
Any idea why is this happening .
Here is a snippet of our entry point code, not complex at all. Putting a breakpoint in the first line of main(), already shows the System.Threading.Thread.CurrentThread.ApartmentState as System.Threading.ApartmentState.MTA.
#region
Main /// <summary> /// The main entry point for the application. /// </summary>[
STAThread] static void Main(){
<some config code. Threading model already set here to MTA>
Application.Run(new frmArticulos(null) );
}

C# 2005 ignoring STAThread attribute
TechNeilogy
LoL... This is even funnier. Will try this evening with one of our old projects, and post the results ;)
J
Jon W
Anyway, as we are now using the same solution as Dede posted above, (ugly, nasty, but effective), we have not much time to dig into this anymore.
Thank you.
J
raduvv
Starting from VS2005 using debug (F5) : MTA
Starting from VS2005 not using debug (Ctrl+F5) : STA
ureyes84
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSENT.v80.en/dv_vsetsa03/html/a3c95130-8e7f-4419-9fcd-b67d077e8efb.htm
apaspula
I tried this on my projects (in VS2005), and they're all set to STA correctly...
[STAThread]
static void Main(string[] args)
{
System.Threading.ApartmentState state =
System.Threading.Thread.CurrentThread.GetApartmentState();
MessageBox.Show(state.ToString());
}
Do you still get the same problem when you create a new project
What is the setting for your application's "Startup Object" (Project Properties, Application page.)
Kamen
Hi.
Thanks for the answer, but we never used ApartmentState property. We already tried with GetApartmentState() method, and the results were exactly the same. The main app thread will run in MTA mode no matter the settings we make (either with the deprecated property or the new method).
Regards
J
pittpanther
mel_24
Thanks for the answer. After long research in the issue, we decided some time ago to use exactly the same solution you propose. During this time, we haven't got any better solution to this. Starting the main app in a newly created thread worked fine, so I think this will stay like this until we move to .NET 3.0.
Thanks for the answer. Regards
J
LordZoster
Satya vani
Creating a new blank project sometimes worked, sometimes don't. I haven't been able to get a pattern. Some old VS2003 project are working fine 'out of the box' with VS2005, while others do show this strange MTA/STA issue. Some new VS2005 projects are working ok, and some others don't.
I've also tried to put the code of one of the old projects in a newly created 2005 project, (moving only the Forms, but using a fresh new startup class, with no ties with the old files, and showing only a 'Hello world' messagebox). Project is failing in initializing STA mode. But removing the old Forms made the same project start nicely in STA mode. The funny part is that the old forms ARE NOT BEING CALLED anywhere in the new project, so their mere pressence in the project triggered the failure. There is only one "main" function, so there is no chance of misplacing the "Startup Object" property.
This is nuts...
J
mabxsi
Tailor
This page has relevant information on this. Search for "apartment" on the page.
http://msdn2.microsoft.com/en-us/netframework/aa497241.aspx
--Carlos
snowmt
So I decided to make a small ugly piece of code that works, but is not pretty nice :
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread th = new Thread(new ThreadStart(ThMain));
th.Name = "Main";
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private static void ThMain()
{
Application.Run(new FrmMain());
}
This code starts my Form in the thread I have defined. So I can manage its properties. I hope there is a better solution...
Dede