Thread apartment states in .NET Core
Hi all! Just wondering, will you support thread apartment state in .NET Core ? Currently there are no support. Maybe in future?
Thanks.
Hi guys. So will you include this functionality into System.Threading.Thread package.
@ericeil
What scenario is motivating this request? This isn't there now because it's not clear what the right way to do it across all OS platforms would be.
@ericeil In case I want to invoke external native function that requires STA state.
@ericeil As for multi-platform support you are right, it's suitable only for windows OS. So in case user tries to set apartment state in Linux OS, I guess the best way will be to throw an exception.
We should consider this when we have a concrete motivating scenario that requires it.
UseShellExecute on standard windows platforms would require a STA thread to call ShellExecute (what it's supposed to do on other platforms is another question, dotnet/corefx#4720 seems to be the place for that discussion).
That said, is there anything that breaks if you launch a thread and P/Invoke to CoInitializeEx with COINIT_APARTMENTTHREADED before the runtime itself calls it?
standard windows platforms would require a STA thread to call ShellExecute
That's a very interesting point. But I wonder if it would be better to handle this detail in the UseShellExecute logic instead? (I.e., have it spin up an STA thread internally, if invoked from an MTA.) That would avoid the need for users to think about this at all (I'm not sure this detail of ShellExecute is common knowledge).
is there anything that breaks if you launch a thread and P/Invoke to CoInitializeEx with COINIT_APARTMENTTHREADED
That should work. But you may need to start the thread by p/invoking to CreateThread, rather than using any .NET method to do it, to avoid the runtime itself CoInitializing the thread before you get a chance.
That should work. But you may need to start the thread by p/invoking to CreateThread, rather than using any .NET method to do it, to avoid the runtime itself CoInitializing the thread before you get a chance.
Uhm, but if the runtime might call CoInitialize when managed code is called then you need a native ThreadProc for CreateThread that calls CoInitializeEx before calling into the runtime. But as far as I can tell the runtime CoInitializes from EnsureComStarted which only seems to be called from COM interop related stuff, so shouldn't it be safe to just start a thread normally by the managed interface?
Other than that, if a manual call to CoInitializeEx is neccessary for the usage of some APIs wouldn't it be a good idea to document exactly when it can be safely called? (Should I make a separate issue for that?)
Just to keep interest alive, PowerShell needs IGroupPolicyObject to implement some cmdlets, but IGroupPolicyObject requires STA. See PowerShell/PowerShell#3353
Given that Windows still has a huge number of COM interfaces this Issue need to reopen.
@jeffbi @iSazonov let's keep the discussion in the new issue. Closed issues are rarely monitored for new comments.