We have a program that can run as a service or as a winforms app. We do different behaviour based on command line parameters passed in.
If we are running as a form, I think we want our entry point to be STAThread.
CA2232: Mark Windows Forms entry points with STAThread
But if we are running as a service do we want our entry point to be an MTAThread? How do people normally deal with this?
We have found some crash dumps (while running as a service ) where we seem to get a stuck finalizer.
We don't get this problem if out main entry point is not marked as an STAThread.
Thread 2:
IP
00:U 00000000779312fa ntdll!NtWaitForSingleObject+0xa
01:U 000007fefd6d10dc KERNELBASE!WaitForSingleObjectEx+0x79
02:U 000007fefdd1e68e ole32!GetToSTA+0x8a
03:U 000007fefde53700 ole32!CRpcChannelBuffer::SwitchAptAndDispatchCall+0x13b
04:U 000007fefde5265b ole32!CRpcChannelBuffer::SendReceive2+0x11b
05:U 000007fefdd0daaa ole32!CAptRpcChnl::SendReceive+0x52
06:U 000007fefdd1cbe6 ole32!CCtxComChnl::SendReceive+0x15c
07:U 000007fefde5205d ole32!NdrExtpProxySendReceive+0x45
08:U 000007fefdb3b949 rpcrt4!NdrpClientCall3+0x2e2
09:U 000007fefde521d0 ole32!ObjectStublessClient+0x11d
0a:U 000007fefdd0d8a2 ole32!ObjectStubless+0x42
0b:U 000007fefdd2ea07 ole32!CObjectContext::InternalContextCallback+0x31537
0c:U 000007fefdd349d1 ole32!CObjectContext::ContextCallback+0x81
0d:U 000007fef4e439b6 clr!CtxEntry::EnterContext+0x232
0e:U 000007fef4e4383c clr!RCW::EnterContext+0x3d
0f:U 000007fef4e437e6 clr! ?? ::FNODOBFM::string'+0x8c449
string'+0x8b99d
10:U 000007fef4e437a9 clr! ?? ::FNODOBFM::
11:U 000007fef4ed326e clr!SyncBlockCache::CleanupSyncBlocks+0xc2
12:U 000007fef4ed319f clr!Thread::DoExtraWorkForFinalizer+0xdc
13:U 000007fef4dfab47 clr!WKS::GCHeap::FinalizerThreadWorker+0x109
14:U 000007fef4d4458c clr!Frame::Pop+0x50
15:U 000007fef4d4451a clr!COMCustomAttribute::PopSecurityContextFrame+0x192
16:U 000007fef4d44491 clr!COMCustomAttribute::PopSecurityContextFrame+0xbd
17:U 000007fef4e21bfe clr!ManagedThreadBase_NoADTransition+0x3f
18:U 000007fef4e21d90 clr!WKS::GCHeap::FinalizerThreadStart+0xb4
19:U 000007fef4da33de clr!Thread::intermediateThreadProc+0x7d
1a:U 00000000777d59ed kernel32!BaseThreadInitThunk+0xd
1b:U 000000007790c541 ntdll!RtlUserThreadStart+0x1d