Skip to content

Commit 9cf8a92

Browse files
authored
AttachVS PR comments fixed (#3201)
1 parent 8b41e37 commit 9cf8a92

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

src/AttachVS/AttachVs.cs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.Diagnostics;
33
using System.Linq;
4-
using System.Linq.Expressions;
54
using System.Reflection;
65
using System.Runtime.CompilerServices;
76
using System.Runtime.InteropServices;
87
using System.Runtime.InteropServices.ComTypes;
98
using System.Threading;
109

11-
namespace Nohwnd.AttachVS
10+
namespace Microsoft.TestPlatform.AttachVS
1211
{
1312
internal class DebuggerUtility
1413
{
@@ -24,9 +23,7 @@ internal static bool AttachVSToProcess(int? pid, int? vsPid)
2423
}
2524
var process = Process.GetProcessById(pid.Value);
2625
Trace($"Using pid: {pid} to get parent VS.");
27-
var vs = GetVsFromPid(vsPid != null
28-
? Process.GetProcessById(vsPid.Value)
29-
: Process.GetProcessById(process.Id));
26+
var vs = GetVsFromPid(Process.GetProcessById(vsPid ?? process.Id));
3027

3128
if (vs != null)
3229
{
@@ -117,6 +114,7 @@ private static bool AttachVs(Process vs, int pid)
117114
object dte, dbg, lps;
118115
runninObjectTable.GetObject(moniker[0], out dte);
119116

117+
// The COM object can be busy, we retry few times, hoping that it won't be busy next time.
120118
for (var i = 0; i < 10; i++)
121119
{
122120
try
@@ -138,7 +136,7 @@ private static bool AttachVs(Process vs, int pid)
138136
}
139137
catch (COMException ex)
140138
{
141-
Trace($"ComException: Tetrying in 250ms.\n{ex}");
139+
Trace($"ComException: Retrying in 250ms.\n{ex}");
142140
Thread.Sleep(250);
143141
}
144142
}
@@ -199,24 +197,17 @@ private static bool IsVsOrNull(Process process)
199197
return true;
200198
}
201199

202-
try
200+
var isVs = process.ProcessName.Equals("devenv", StringComparison.InvariantCultureIgnoreCase);
201+
if (isVs)
203202
{
204-
var isVs = process.ProcessName.Equals("devenv", StringComparison.InvariantCultureIgnoreCase);
205-
if (isVs)
206-
{
207-
Trace($"Process {process.ProcessName} ({process.Id}) is VS.");
208-
}
209-
else
210-
{
211-
Trace($"Process {process.ProcessName} ({process.Id}) is not VS.");
212-
}
213-
214-
return isVs;
203+
Trace($"Process {process.ProcessName} ({process.Id}) is VS.");
215204
}
216-
catch
205+
else
217206
{
218-
return true;
207+
Trace($"Process {process.ProcessName} ({process.Id}) is not VS.");
219208
}
209+
210+
return isVs;
220211
}
221212

222213
private static bool IsCorrectParent(Process currentProcess, Process parent)
@@ -229,12 +220,9 @@ private static bool IsCorrectParent(Process currentProcess, Process parent)
229220
{
230221
return true;
231222
}
232-
else
233-
{
234-
Trace($"Process {parent.ProcessName} ({parent.Id}) is not a valid parent because it started after the current process.");
235-
return false;
236-
}
237223

224+
Trace($"Process {parent.ProcessName} ({parent.Id}) is not a valid parent because it started after the current process.");
225+
return false;
238226
}
239227
catch
240228
{

src/AttachVS/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Linq;
33

4-
namespace Nohwnd.AttachVS.Console
4+
namespace Microsoft.TestPlatform.AttachVS
55
{
66
internal class Program
77
{

0 commit comments

Comments
 (0)