dotnet / dotnet/dotnet-api-docs

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

Offen
#9,729 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
area-System.Diagnostics.Process Pri3 untriaged
Vorherrschende Sprache
C#
Sterne
949
Forks
1.7k
Ø Merge
2 T. 19 Std.
Gemergte PRs (30 T.)
52

Beschreibung

Hi,

According to the documentations, [`System.Diagnostics.Process.Start`](https://learn.microsoft.com/en-us/dotnet/api/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](https://stackoverflow.com/questions/33042010/in-what-cases-does-the-process-start-method-return-false) 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](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs), 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`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw) is [called](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs#L156C56-L156C71) and [`true` is returned](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs#L96) if `hProcess` is set on [`SHELLEXECUTEINFOW`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-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](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L556) [`CreateProcessWithLogonW`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createprocesswithlogonw) when a custom user is used or [at](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L556) [`CreateProcessA`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa) when this is not the case. Then `false` is returned if [an invalid process handle was returned by the underlying function](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L650).

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!

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginnen Sie mit der Dokumentation der API System.Diagnostics.Process.Start und vergleichen Sie deren Formulierung zum Rückgabewert mit den verlinkten Implementierungen Process.cs, Process.Win32.cs und Process.Windows.cs. Verfolgen Sie die ShellExecute- und Nicht-ShellExecute-Pfade und dokumentieren Sie anschließend die unterstützten Bedingungen für eine Rückgabe von false sowie ein Beispiel, falls die Implementierung eines unterstützt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
28/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.