11using System ;
22using System . Linq ;
3+ using System . Threading . Tasks ;
34using System . Collections . Generic ;
45using System . ComponentModel . Composition ;
56using GitHub . Models ;
89using GitHub . TeamFoundation . Services ;
910using Serilog ;
1011using Microsoft . VisualStudio . TeamFoundation . Git . Extensibility ;
11- using Task = System . Threading . Tasks . Task ;
1212
1313namespace GitHub . VisualStudio . Base
1414{
@@ -24,7 +24,6 @@ public class VSGitExt : IVSGitExt
2424 readonly IGitHubServiceProvider serviceProvider ;
2525 readonly IVSUIContext context ;
2626 readonly ILocalRepositoryModelFactory repositoryFactory ;
27- readonly object refreshLock = new object ( ) ;
2827
2928 IGitExt gitService ;
3029 IReadOnlyList < ILocalRepositoryModel > activeRepositories ;
@@ -41,23 +40,24 @@ public VSGitExt(IGitHubServiceProvider serviceProvider, IVSUIContextFactory fact
4140 this . repositoryFactory = repositoryFactory ;
4241
4342 // The IGitExt service isn't available when a TFS based solution is opened directly.
44- // It will become available when moving to a Git based solution (cause a UIContext event to fire).
43+ // It will become available when moving to a Git based solution (and cause a UIContext event to fire).
4544 context = factory . GetUIContext ( new Guid ( Guids . GitSccProviderId ) ) ;
4645
4746 // Start with empty array until we have a change to initialize.
4847 ActiveRepositories = Array . Empty < ILocalRepositoryModel > ( ) ;
4948
49+ InitializeTask = Task . CompletedTask ;
50+
5051 if ( context . IsActive && TryInitialize ( ) )
5152 {
5253 // Refresh ActiveRepositories on background thread so we don't delay startup.
53- InitializeTask = Task . Run ( ( ) => RefreshActiveRepositories ( ) ) ;
54+ QueueRefreshActiveRepositories ( ) ;
5455 }
5556 else
5657 {
5758 // If we're not in the UIContext or TryInitialize fails, have another go when the UIContext changes.
5859 context . UIContextChanged += ContextChanged ;
5960 log . Debug ( "VSGitExt will be initialized later" ) ;
60- InitializeTask = Task . CompletedTask ;
6161 }
6262 }
6363
@@ -68,7 +68,7 @@ void ContextChanged(object sender, VSUIContextChangedEventArgs e)
6868 if ( e . Activated && TryInitialize ( ) )
6969 {
7070 // Refresh ActiveRepositories on background thread so we don't delay UI context change.
71- InitializeTask = Task . Run ( ( ) => RefreshActiveRepositories ( ) ) ;
71+ QueueRefreshActiveRepositories ( ) ;
7272 context . UIContextChanged -= ContextChanged ;
7373 log . Debug ( "Initialized VSGitExt on UIContextChanged" ) ;
7474 }
@@ -83,7 +83,7 @@ bool TryInitialize()
8383 {
8484 if ( e . PropertyName == nameof ( gitService . ActiveRepositories ) )
8585 {
86- RefreshActiveRepositories ( ) ;
86+ QueueRefreshActiveRepositories ( ) ;
8787 }
8888 } ;
8989
@@ -95,19 +95,22 @@ bool TryInitialize()
9595 return false ;
9696 }
9797
98+ void QueueRefreshActiveRepositories ( )
99+ {
100+ // Execute tasks in sequence on thread pool.
101+ InitializeTask = InitializeTask . ContinueWith ( _ => RefreshActiveRepositories ( ) , TaskScheduler . Default ) ;
102+ }
103+
98104 void RefreshActiveRepositories ( )
99105 {
100106 try
101107 {
102- lock ( refreshLock )
103- {
104- log . Debug (
105- "IGitExt.ActiveRepositories (#{Id}) returned {Repositories}" ,
106- gitService . GetHashCode ( ) ,
107- gitService ? . ActiveRepositories . Select ( x => x . RepositoryPath ) ) ;
108+ log . Debug (
109+ "IGitExt.ActiveRepositories (#{Id}) returned {Repositories}" ,
110+ gitService . GetHashCode ( ) ,
111+ gitService ? . ActiveRepositories . Select ( x => x . RepositoryPath ) ) ;
108112
109- ActiveRepositories = gitService ? . ActiveRepositories . Select ( x => repositoryFactory . Create ( x . RepositoryPath ) ) . ToList ( ) ;
110- }
113+ ActiveRepositories = gitService ? . ActiveRepositories . Select ( x => repositoryFactory . Create ( x . RepositoryPath ) ) . ToList ( ) ;
111114 }
112115 catch ( Exception e )
113116 {
0 commit comments