C# 2005 ignoring STAThread attribute

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) );

}



Answer this question

C# 2005 ignoring STAThread attribute

  • TechNeilogy

    Dede wrote:
    I tried something like this...

    Starting from VS2005 using debug (F5) : MTA
    Starting from VS2005 not using debug (Ctrl+F5) : STA


    LoL... This is even funnier. Will try this evening with one of our old projects, and post the results ;)

    J

  • Jon W

    As I said in a previous post, this is only happening sometimes, but I haven’t been able to get the pattern here. Seems related to Windows Forms, but... I cannot say.

    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

    I tried something like this...

    Starting from VS2005 using debug (F5) : MTA
    Starting from VS2005 not using debug (Ctrl+F5) : STA

  • ureyes84

    check this link may be this link useful for you...


    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSENT.v80.en/dv_vsetsa03/html/a3c95130-8e7f-4419-9fcd-b67d077e8efb.htm

  • apaspula

    There's something funny going on here.

    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

    Peter Ritchie wrote:
    Follow the instructions in the compiler warning you get when you use the ApartmentState property: it's deprecated, use GetApartmentState() instead.

    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

    Just like Matthew I also have no problem using GetApartmentState()...

  • mel_24

    Dede wrote:
    I have the same problem and long hours on search engines didn't help me learn something.


    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


    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

    Follow the instructions in the compiler warning you get when you use the ApartmentState property: it's deprecated, use GetApartmentState() instead.

  • Satya vani

    Obviously, the Startup Object is set to the class hosting the "main" function (in my case, a Windows Form, as this is a project carried over from VS 2003 and .NET 1.1).

    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

    Jorge P wrote:
    As I said in a previous post, this is only happening sometimes, but I haven’t been able to get the pattern here. Seems related to Windows Forms, but... I cannot say.

    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
    I think the problem you've encountered is a symptom of a much bigger problem. The workaround you've found may help with that particular symptom but you may encounter other symptoms. I encourage you to report the problem to customer support in an attempt to find out what the real problem is.

  • 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

    I have the same problem and long hours on search engines didn't help me learn something.


    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

  • C# 2005 ignoring STAThread attribute