1

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 support the [STAThread] directive, it only has the [MTAThread]. Still, I never encountered any problems there.

Now, to take it 1 step furhter, you can also run Compact Framework applications on a full blown windows system. I think that in that case the full blown .NET framework will be used.

At that point, we are running code with an MTA apartment state, which uses windows forms on a full blown windows system. This brings me to the question, if a compact framework application runs on a full blown windows operating system, why doesn't it complain about the lacking [STAThread] directive ?

bvdb
  • 22,839
  • 10
  • 110
  • 123

1 Answers1

1

As per This Answer I'd say it's not actually Windows Forms running on the normal machine but a subset. It could of course also be that some sort of proxying is going on and the framework is taking care of bootstrapping an STA thread and marshalling back and forth for you.

A comment on This Question specifically:

Simple CF apps will run without modification on desktop, this is well known. But apps that use CF-only APIs (ie aygshell related), will not run. The STAThread compile option can only be used during compile (AFAIK). If a DLL was compiled with that option I can imagine that it wil not run on desktop. For the web browser component, you need to not load this hardcoded but from within code and then load either the full framework one or the CF one. CF Forms are hardcoded to MTA. If a COM is loaded that requires STA, it will give exception.

By Josef, seems to indicate that the compact framework forms will happily trundle along under MTA, but freak out as soon as you use a COM-based component from winforms that does require STA.

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
  • 2
    See https://social.msdn.microsoft.com/Forums/en-US/d5b637f4-56f6-417a-a004-851c8e7ca01f/stathread-can-not-be-found-but-where-is-it?forum=netfxcompact "No, you're not missing anything - there's no such attribute on NETCF and there's no way to force STA behavior. MTA is the only supported mode and generally you don’t need STA on NETCF. " – josef Jan 06 '17 at 05:47