dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

More information on false return value of System.Diagnostics.Process.Start

Open ArthurHNL opened this issue 11 months ago • 1 comments

Hi,

According to the documentations, System.Diagnostics.Process.Start can return false when a process is 'reused', but does not really give an indication when this happens. I'm currently implementing a feature where a process is started and I need to know if and how I should handle a false return

A relevant thread on StackOverflow goes a bit deeper into the actual implementation, but it this thread cannot really give an example on when it will return false.

If I look at the actual implementation on .NET myself, it looks like Start calls StartCore after doing some sanity checks. StartCore is platform-dependent an behaves differently on whether or not ShellExecute is used.

If ShellExecute is used, eventually ShellExecuteXw is called and true is returned if hProcess is set on SHELLEXECUTEINFOW. hProcess will be NULL when no process is launched when URLS/documents are opened and eg. the Web browser is already running. Could this be the 'reusing' of a process?

If ShellExecute is not used, the call eventually ends up at either CreateProcessWithLogonW when a custom user is used or at CreateProcessA when this is not the case. Then false is returned if an invalid process handle was returned by the underlying function.

The case for ShellExecute is somewhat clear, but the case for non shell execute is not. At least not to me. Why would an invalid handle being returned by the kernel indicate that a process was reused?

Can some more information regarding this be provided? I've personally never heard of processes being 'reused' by the OS when starting a process. Maybe add an example for which the method will always return false?

Thanks in advance!

ArthurHNL avatar Mar 15 '24 11:03 ArthurHNL

It seems the "can return false" is a Windows-ism. The Unix implementation (i.e. anything that isn't Windows) will always return true. These Windows-isms are all too common in the documentation, it seems.

colejohnson66 avatar Mar 22 '24 11:03 colejohnson66