11using System ;
22using System . Diagnostics ;
33using System . Linq ;
4- using System . Linq . Expressions ;
54using System . Reflection ;
65using System . Runtime . CompilerServices ;
76using System . Runtime . InteropServices ;
87using System . Runtime . InteropServices . ComTypes ;
98using 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 {
0 commit comments