From 78573751cf9b3414cbc49ca6958fce54f300d751 Mon Sep 17 00:00:00 2001 From: Anze Vodovnik Date: Mon, 23 Nov 2020 10:51:42 +0100 Subject: [PATCH] Changes as per PR Comments. --- .../src/System/Diagnostics/Process.cs | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs index a1ccddef5fb79..b7e925f535a98 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs @@ -1343,15 +1343,30 @@ private void StopWatchingForExit() public override string ToString() { - if (Associated && !HasExited) + string result = base.ToString(); + + try { - string processName = ProcessName; - if (processName.Length != 0) + if (Associated) { - return string.Format(CultureInfo.CurrentCulture, "{0} ({1})", base.ToString(), processName); + _processInfo ??= ProcessManager.GetProcessInfo(_processId, _machineName); + if (_processInfo is not null) + { + string processName = _processInfo.ProcessName; + if (processName.Length != 0) + { + result = $"{result} ({processName})"; + } + } } } - return base.ToString(); + catch + { + // Handle cases where a process would exit straight after our checks + // and/or make ProcessManager throw an exception. + } + + return result; } ///