runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Thread apartment states in .NET Core

Open blobor opened this issue 11 years ago • 12 comments

Hi all! Just wondering, will you support thread apartment state in .NET Core ? Currently there are no support. Maybe in future?

Thanks.

blobor avatar Jan 26 '15 04:01 blobor

Hi guys. So will you include this functionality into System.Threading.Thread package.

blobor avatar Feb 27 '15 09:02 blobor

@ericeil

joshfree avatar Oct 05 '15 16:10 joshfree

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 avatar Oct 05 '15 18:10 ericeil

@ericeil In case I want to invoke external native function that requires STA state.

blobor avatar Oct 06 '15 08:10 blobor

@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.

blobor avatar Oct 06 '15 08:10 blobor

We should consider this when we have a concrete motivating scenario that requires it.

ericeil avatar Oct 09 '15 17:10 ericeil

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?

poizan42 avatar Apr 08 '16 12:04 poizan42

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.

ericeil avatar Apr 08 '16 19:04 ericeil

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

poizan42 avatar Apr 09 '16 22:04 poizan42

Just to keep interest alive, PowerShell needs IGroupPolicyObject to implement some cmdlets, but IGroupPolicyObject requires STA. See PowerShell/PowerShell#3353

jeffbi avatar Mar 28 '17 01:03 jeffbi

Given that Windows still has a huge number of COM interfaces this Issue need to reopen.

iSazonov avatar Mar 28 '17 03:03 iSazonov

@jeffbi @iSazonov let's keep the discussion in the new issue. Closed issues are rarely monitored for new comments.

karelz avatar Mar 31 '17 02:03 karelz