From 9cf8a92a863963b14cf686009f2a20d6698c0882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 29 Nov 2021 15:34:31 +0100 Subject: [PATCH] AttachVS PR comments fixed (#3201) --- src/AttachVS/AttachVs.cs | 38 +++++++++++++------------------------- src/AttachVS/Program.cs | 2 +- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/AttachVS/AttachVs.cs b/src/AttachVS/AttachVs.cs index e456d773f3..8c2235f22e 100644 --- a/src/AttachVS/AttachVs.cs +++ b/src/AttachVS/AttachVs.cs @@ -1,14 +1,13 @@ using System; using System.Diagnostics; using System.Linq; -using System.Linq.Expressions; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Threading; -namespace Nohwnd.AttachVS +namespace Microsoft.TestPlatform.AttachVS { internal class DebuggerUtility { @@ -24,9 +23,7 @@ internal static bool AttachVSToProcess(int? pid, int? vsPid) } var process = Process.GetProcessById(pid.Value); Trace($"Using pid: {pid} to get parent VS."); - var vs = GetVsFromPid(vsPid != null - ? Process.GetProcessById(vsPid.Value) - : Process.GetProcessById(process.Id)); + var vs = GetVsFromPid(Process.GetProcessById(vsPid ?? process.Id)); if (vs != null) { @@ -117,6 +114,7 @@ private static bool AttachVs(Process vs, int pid) object dte, dbg, lps; runninObjectTable.GetObject(moniker[0], out dte); + // The COM object can be busy, we retry few times, hoping that it won't be busy next time. for (var i = 0; i < 10; i++) { try @@ -138,7 +136,7 @@ private static bool AttachVs(Process vs, int pid) } catch (COMException ex) { - Trace($"ComException: Tetrying in 250ms.\n{ex}"); + Trace($"ComException: Retrying in 250ms.\n{ex}"); Thread.Sleep(250); } } @@ -199,24 +197,17 @@ private static bool IsVsOrNull(Process process) return true; } - try + var isVs = process.ProcessName.Equals("devenv", StringComparison.InvariantCultureIgnoreCase); + if (isVs) { - var isVs = process.ProcessName.Equals("devenv", StringComparison.InvariantCultureIgnoreCase); - if (isVs) - { - Trace($"Process {process.ProcessName} ({process.Id}) is VS."); - } - else - { - Trace($"Process {process.ProcessName} ({process.Id}) is not VS."); - } - - return isVs; + Trace($"Process {process.ProcessName} ({process.Id}) is VS."); } - catch + else { - return true; + Trace($"Process {process.ProcessName} ({process.Id}) is not VS."); } + + return isVs; } private static bool IsCorrectParent(Process currentProcess, Process parent) @@ -229,12 +220,9 @@ private static bool IsCorrectParent(Process currentProcess, Process parent) { return true; } - else - { - Trace($"Process {parent.ProcessName} ({parent.Id}) is not a valid parent because it started after the current process."); - return false; - } + Trace($"Process {parent.ProcessName} ({parent.Id}) is not a valid parent because it started after the current process."); + return false; } catch { diff --git a/src/AttachVS/Program.cs b/src/AttachVS/Program.cs index c56f2320b7..dc57adea49 100644 --- a/src/AttachVS/Program.cs +++ b/src/AttachVS/Program.cs @@ -1,7 +1,7 @@ using System; using System.Linq; -namespace Nohwnd.AttachVS.Console +namespace Microsoft.TestPlatform.AttachVS { internal class Program {