@@ -195,7 +195,7 @@ private static Type GetObjectType()
195195 Type objectType = null ;
196196
197197 // locals
198- double versionNumber = 17 ;
198+ double versionNumber = 18 ;
199199 string programId = "" ;
200200
201201 do
@@ -309,21 +309,25 @@ private static bool RemoveFileIfFileExistsInProject(EnvDTE.Project project, Proj
309309
310310 // local (Com objects start at 1)
311311 int index = 0 ;
312-
312+
313313 // iterate project items
314314 foreach ( ProjectItem projectItem in projectItems )
315315 {
316- // Increment the value for tempIndex
316+ // Increment the value for index
317317 index ++ ;
318318
319319 // if the name matches
320320 if ( projectItem . Name == projectFile . FileName )
321321 {
322- // Remove this item
323- projectItems . Item ( index ) . Remove ( ) ;
322+ // if this is not the Gateway
323+ if ( ! projectFile . FileName . Contains ( "Gateway" ) )
324+ {
325+ // Remove this item
326+ projectItems . Item ( index ) . Remove ( ) ;
324327
325- // Set to true
326- fileRemoved = true ;
328+ // Set to true
329+ fileRemoved = true ;
330+ }
327331
328332 // break out of loop
329333 break ;
@@ -346,6 +350,62 @@ private static bool RemoveFileIfFileExistsInProject(EnvDTE.Project project, Proj
346350 return fileRemoved ;
347351 }
348352 #endregion
353+
354+ #region SafeGetProjectItems(EnvDTE.Project project)
355+ /// <summary>
356+ /// method returns the Get Project Items
357+ /// </summary>
358+ private static EnvDTE . ProjectItems SafeGetProjectItems ( EnvDTE . Project project )
359+ {
360+ // initial value
361+ EnvDTE . ProjectItems projectItems = null ;
362+
363+ // if the project exists
364+ if ( project != null )
365+ {
366+ try
367+ {
368+ projectItems = project . ProjectItems ;
369+ }
370+ catch
371+ {
372+ // swallow; COM can throw until fully ready
373+ }
374+ }
375+
376+ // return value
377+ return projectItems ;
378+ }
379+ #endregion
380+
381+ #region TryTouchProject(EnvDTE.Project project)
382+ /// <summary>
383+ /// method returns the Touch Project
384+ /// </summary>
385+ private static void TryTouchProject ( EnvDTE . Project project )
386+ {
387+ // if the project exists
388+ if ( project != null )
389+ {
390+ try
391+ {
392+ // Touching these often triggers load completion
393+ string fullName = project . FullName ;
394+ object projectObject = project . Object ;
395+
396+ EnvDTE . Properties properties = project . Properties ;
397+ if ( properties != null )
398+ {
399+ int propertyCount = properties . Count ;
400+ }
401+ }
402+ catch
403+ {
404+ // ignore; we're only nudging
405+ }
406+ }
407+ }
408+ #endregion
349409
350410 #region UpdateProject(EnvDTE.Project project, IList<ProjectFile> files, DataManager.ProjectTypeEnum projectType)
351411 /// <summary>
@@ -454,6 +514,9 @@ internal static bool UpdateSolution(VisualStudioSolution visualStudioSolution, s
454514 // each project needs to be updated
455515 foreach ( EnvDTE . Project project in solution . Projects )
456516 {
517+ // wait for the projectItems
518+ WaitUntilProjectItemsReady ( project , 2000 , 100 ) ;
519+
457520 // if this project name is the ApplicationLogicComponen project
458521 if ( project . Name == visualStudioSolution . ApplicationLogicComponentProjectName )
459522 {
@@ -556,6 +619,56 @@ internal static bool UpdateSolution(VisualStudioSolution visualStudioSolution, s
556619 }
557620 #endregion
558621
622+ #region WaitForProjectItems(EnvDTE.Project project, int timeoutMs, int pollMs)
623+ /// <summary>
624+ /// method returns the For Project Items
625+ /// </summary>
626+ private static bool WaitUntilProjectItemsReady ( EnvDTE . Project project , int timeoutMilliseconds , int pollMilliseconds )
627+ {
628+ // initial value
629+ bool ready = false ;
630+
631+ // if the project exists
632+ if ( project != null )
633+ {
634+ DateTime endTime = DateTime . UtcNow . AddMilliseconds ( timeoutMilliseconds ) ;
635+
636+ while ( DateTime . UtcNow < endTime )
637+ {
638+ // Nudge project to complete initialization
639+ TryTouchProject ( project ) ;
640+
641+ EnvDTE . ProjectItems projectItems = SafeGetProjectItems ( project ) ;
642+
643+ // if the projectItems exist
644+ if ( projectItems != null )
645+ {
646+ ready = true ;
647+ break ;
648+ }
649+
650+ System . Windows . Forms . Application . DoEvents ( ) ;
651+ System . Threading . Thread . Sleep ( pollMilliseconds ) ;
652+ }
653+
654+ // final attempt after loop
655+ if ( ! ready )
656+ {
657+ TryTouchProject ( project ) ;
658+ EnvDTE . ProjectItems finalItems = SafeGetProjectItems ( project ) ;
659+
660+ if ( finalItems != null )
661+ {
662+ ready = true ;
663+ }
664+ }
665+ }
666+
667+ // return value
668+ return ready ;
669+ }
670+ #endregion
671+
559672 #endregion
560673
561674 }
0 commit comments