dotnet / dotnet/dotnet-api-docs

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

Aperta
#9,729 1 commento 1 reazione 0 assegnatari Vedi su GitHub
area-System.Diagnostics.Process Pri3 untriaged
Lingua principale
C#
Stelle
949
Fork
1.7k
Merge medio
2g 19h
PR unite (30g)
52

Descrizione

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!

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dalla documentazione dell'API System.Diagnostics.Process.Start e confronta la formulazione del valore restituito con le implementazioni collegate Process.cs, Process.Win32.cs e Process.Windows.cs. Traccia i percorsi ShellExecute e non-ShellExecute, quindi documenta le condizioni supportate per una restituzione di false e un esempio, se l'implementazione ne supporta uno.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
28/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.