Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions src/AttachVS/AttachVs.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/AttachVS/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace Nohwnd.AttachVS.Console
namespace Microsoft.TestPlatform.AttachVS
{
internal class Program
{
Expand Down