PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : default ApartmentState einer C# application?


Elemental
2005-04-21, 16:07:06
Hallo allerseits,
ich habe diese Frage schon bei codeproject.com gepostet, deshalb copy&paste ich einfach mal hierher. sorry fürs englisch:


Hi all,
does anybody know what the default ApartmentState of a C# application is, if you do not set the [STAthread] attribute for your main method?

I have read different statements in the Internet. Some say that the default ApartmentState for a C# application is "ApartmentState.MTA", whereas for VB.NET it's "ApartmentState.STA".

Others say that the default state is "ApartmentState.Unknown".


Well, I tried it out myself using a simple C# app, but what I found was not really good:
Almost every computer had "ApartmentState.Unknown" as default, but unfortunately some had "ApartmentState.MTA".
What influences this behavior?


The reason why I'm asking is because I have an application which uses CoInitlizeSecurity and DragAndDrop.

The app starts like this:

public static int Main(string[] args)
{
System.Threading.Thread.CurrentThread.ApartmentState = ApartmentState.STA;
int nHresult = CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, IntPtr.Zero, 0, IntPtr.Zero);

...
}



This almost ever works. But on some computers that have MTA as default ApartmentState, I cannot reset the CurrentThread's ApartmentState to STA, because this can be set only once. The consequence is that "Drag and Drop Registration" fails, because Drag and Drop requires STA.


When I put the [STAThread] attribute to my main method, then the CurrentThread's ApartmentState is STA on every computer, but CoInitializeSecurity returns RPC_E_TOO_LATE.


Now is there something that I can do to solve this problem?
thanks in advance for your help!



Ich hoffe hier gibts jemand, der mir weiterhelfen kann.

mfG
Elemental