1616using Alphaleonis . VSProjectSetMgr . ViewModels . Nodes ;
1717using System . ComponentModel ;
1818using System . IO ;
19-
19+ using System . Threading ;
20+ using System . Threading . Tasks ;
21+ using Task = System . Threading . Tasks . Task ;
22+ using System . Windows . Threading ;
2023
2124namespace Alphaleonis . VSProjectSetMgr
2225{
@@ -25,17 +28,17 @@ public interface ISettingsProvider : System.IServiceProvider
2528 ProjectSetManagerUserOptions GetSettings ( ) ;
2629 }
2730
28- [ PackageRegistration ( UseManagedResourcesOnly = true ) ]
31+ [ PackageRegistration ( UseManagedResourcesOnly = true , AllowsBackgroundLoading = true ) ]
2932 [ InstalledProductRegistration ( "#110" , "#112" , "1.0" , IconResourceID = 400 ) ]
3033 [ ProvideMenuResource ( "Menus.ctmenu" , 1 ) ]
3134 [ Guid ( GuidList . guidLoadedProjectsProfileManagerPkgString ) ]
3235 [ ProvideToolWindow ( typeof ( ProjectSetManagerToolWindow ) ) ]
33- [ ProvideAutoLoad ( "{F1536EF8-92EC-443C-9ED7-FDADF150DA82}" ) ]
34- [ ProvideService ( typeof ( SProjectSetRepository ) ) ]
35- [ ProvideService ( typeof ( SInteractionService ) ) ]
36+ [ ProvideAutoLoad ( "{F1536EF8-92EC-443C-9ED7-FDADF150DA82}" , PackageAutoLoadFlags . BackgroundLoad ) ]
37+ [ ProvideService ( typeof ( SProjectSetRepository ) , IsAsyncQueryable = true ) ]
38+ [ ProvideService ( typeof ( SInteractionService ) , IsAsyncQueryable = true ) ]
3639 [ ProvideOptionPage ( typeof ( ProjectSetManagerUserOptions ) , "Project Set Manager" , "General" , 0 , 0 , true ) ]
3740 [ ProvideProfileAttribute ( typeof ( ProjectSetManagerUserOptions ) , "Project Set Manager" , "General" , 0 , 0 , true ) ]
38- public sealed class LoadedProjectsProfileManagerPackage : Package , IVsSolutionEvents , ISettingsProvider
41+ public sealed class LoadedProjectsProfileManagerPackage : AsyncPackage , IVsSolutionEvents , ISettingsProvider
3942 {
4043 private uint m_eventsCookie ;
4144 private const string OptionSolutionProfiles = "SolutionProfiles" ;
@@ -53,11 +56,11 @@ public sealed class LoadedProjectsProfileManagerPackage : Package, IVsSolutionEv
5356 public LoadedProjectsProfileManagerPackage ( )
5457 {
5558 Debug . WriteLine ( string . Format ( CultureInfo . CurrentCulture , "Entering constructor for: {0}" , this . ToString ( ) ) ) ;
56- var container = this as IServiceContainer ;
59+
5760 m_interactionService = new InteractionService ( this ) ;
58- container . AddService ( typeof ( SInteractionService ) , m_interactionService , true ) ;
61+ AddService ( typeof ( SInteractionService ) , ( s , c , t ) => Task . FromResult ( ( object ) m_interactionService ) , true ) ;
5962 m_repository = new ProjectSetRepository ( ) ;
60- container . AddService ( typeof ( SProjectSetRepository ) , m_repository , true ) ;
63+ AddService ( typeof ( SProjectSetRepository ) , ( s , c , t ) => Task . FromResult ( ( object ) m_repository ) , true ) ;
6164 }
6265
6366 #region Package Members
@@ -66,13 +69,16 @@ public LoadedProjectsProfileManagerPackage()
6669 /// Initialization of the package; this method is called right after the package is sited, so this is the place
6770 /// where you can put all the initialization code that rely on services provided by VisualStudio.
6871 /// </summary>
69- protected override void Initialize ( )
72+ protected override async Task InitializeAsync ( CancellationToken cancellationToken , IProgress < ServiceProgressData > progress )
7073 {
71- base . Initialize ( ) ;
74+ await base . InitializeAsync ( cancellationToken , progress ) ;
7275 AddOptionKey ( OptionSolutionProfiles ) ;
7376
77+ // Switches to the UI thread in order to consume some services used in command initialization
78+ await JoinableTaskFactory . SwitchToMainThreadAsync ( cancellationToken ) ;
79+
7480 // Add our command handlers for menu (commands must exist in the .vsct file)
75- OleMenuCommandService mcs = GetService ( typeof ( IMenuCommandService ) ) as OleMenuCommandService ;
81+ OleMenuCommandService mcs = await GetServiceAsync ( typeof ( IMenuCommandService ) ) as OleMenuCommandService ;
7682 if ( null != mcs )
7783 {
7884 for ( int i = 0 ; i < MRUSize ; i ++ )
@@ -95,6 +101,8 @@ protected override void Initialize()
95101
96102 public void OnExecuteUnloadAllProjectsInSolution ( object sender , EventArgs args )
97103 {
104+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
105+
98106 foreach ( Assembly asm in AppDomain . CurrentDomain . GetAssemblies ( ) )
99107 {
100108 Debug . WriteLine ( asm . FullName ) ;
@@ -112,6 +120,8 @@ public void OnExecuteUnloadAllProjectsInSolution(object sender, EventArgs args)
112120
113121 public void OnQueryStatusUnloadAllProjectsInSolution ( object sender , EventArgs args )
114122 {
123+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
124+
115125 OleMenuCommand command = sender as OleMenuCommand ;
116126 if ( command != null )
117127 {
@@ -131,6 +141,8 @@ public void OnQueryStatusUnloadAllProjectsInSolution(object sender, EventArgs ar
131141
132142 public void OnExecuteLoadAllProjectsInSolution ( object sender , EventArgs args )
133143 {
144+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
145+
134146 SolutionManager solMgr = GetSolutionManager ( ) ;
135147 if ( solMgr != null )
136148 {
@@ -143,6 +155,8 @@ public void OnExecuteLoadAllProjectsInSolution(object sender, EventArgs args)
143155
144156 public void OnQueryStatusLoadAllProjectsInSolution ( object sender , EventArgs args )
145157 {
158+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
159+
146160 OleMenuCommand command = sender as OleMenuCommand ;
147161 if ( command != null )
148162 {
@@ -185,6 +199,8 @@ private void OnMRUQueryStatus(object sender, EventArgs e)
185199
186200 private void OnExecuteMRULoadExclusive ( object sender , EventArgs e )
187201 {
202+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
203+
188204 var projectSet = GetMRUEntry ( sender , PkgCmdIDList . cmdidLoadExclusiveMRU0 ) ;
189205 if ( projectSet != null )
190206 {
@@ -198,6 +214,8 @@ private void OnExecuteMRULoadExclusive(object sender, EventArgs e)
198214
199215 private void OnExecuteMRULoad ( object sender , EventArgs e )
200216 {
217+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
218+
201219 var projectSet = GetMRUEntry ( sender , PkgCmdIDList . cmdidLoadMRU0 ) ;
202220 if ( projectSet != null )
203221 {
@@ -211,6 +229,8 @@ private void OnExecuteMRULoad(object sender, EventArgs e)
211229
212230 private void OnExecuteMRUUnload ( object sender , EventArgs e )
213231 {
232+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
233+
214234 var projectSet = GetMRUEntry ( sender , PkgCmdIDList . cmdidUnloadMRU0 ) ;
215235 if ( projectSet != null )
216236 {
@@ -224,6 +244,8 @@ private void OnExecuteMRUUnload(object sender, EventArgs e)
224244
225245 private void OnExecuteMRUUnloadExclusive ( object sender , EventArgs e )
226246 {
247+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
248+
227249 var projectSet = GetMRUEntry ( sender , PkgCmdIDList . cmdidUnloadExclusiveMRU0 ) ;
228250 if ( projectSet != null )
229251 {
@@ -252,6 +274,8 @@ private ProjectSet GetMRUEntry(object sender, int baseCommandId)
252274
253275 private void OnExecuteShowManagerToolWindow ( object sender , EventArgs e )
254276 {
277+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
278+
255279 // Get the instance number 0 of this tool window. This window is single instance so this instance
256280 // is actually the only one.
257281 // The last flag is set to true so that if the tool window does not exists it will be created.
@@ -268,16 +292,21 @@ private void OnExecuteShowManagerToolWindow(object sender, EventArgs e)
268292
269293 protected override void OnLoadOptions ( string key , System . IO . Stream stream )
270294 {
295+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
296+
271297 if ( key . Equals ( OptionSolutionProfiles ) )
272298 {
273299 var settings = GetSettings ( ) ;
274- try
300+ if ( settings . Storage == ProjectSetProfileStorage . Solution )
275301 {
276- m_repository . LoadBinary ( stream , GetSolutionManager ( ) ) ;
277- }
278- catch ( Exception ex )
279- {
280- m_interactionService . ShowError ( "Error loading profile configuration from solution:\r \n {0}" , ex . Message ) ;
302+ try
303+ {
304+ m_repository . LoadBinary ( stream , GetSolutionManager ( ) ) ;
305+ }
306+ catch ( Exception ex )
307+ {
308+ m_interactionService . ShowError ( "Error loading profile configuration from solution:\r \n {0}" , ex . Message ) ;
309+ }
281310 }
282311 }
283312 else
@@ -293,6 +322,8 @@ public ProjectSetManagerUserOptions GetSettings()
293322
294323 protected override void OnSaveOptions ( string key , System . IO . Stream stream )
295324 {
325+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
326+
296327 if ( key . Equals ( OptionSolutionProfiles ) )
297328 {
298329 var settings = GetSettings ( ) ;
@@ -326,6 +357,8 @@ protected override void OnSaveOptions(string key, System.IO.Stream stream)
326357
327358 private void SaveOptionsExternal ( )
328359 {
360+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
361+
329362 SolutionManager solMgr = GetSolutionManager ( ) ;
330363 if ( solMgr != null )
331364 {
@@ -410,6 +443,8 @@ private static bool AreFilesIdentical(string file1, string file2, int bufferSize
410443
411444 private void LoadOptionsExternal ( )
412445 {
446+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
447+
413448 SolutionManager solMgr = GetSolutionManager ( ) ;
414449 if ( solMgr != null )
415450 {
@@ -435,6 +470,8 @@ private void LoadOptionsExternal()
435470
436471 private SolutionManager GetSolutionManager ( )
437472 {
473+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
474+
438475 IVsSolution solution = ( IVsSolution ) GetService ( typeof ( SVsSolution ) ) ;
439476 if ( null != solution )
440477 {
@@ -446,6 +483,8 @@ private SolutionManager GetSolutionManager()
446483
447484 private void AdviseSolutionEvents ( )
448485 {
486+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
487+
449488 var solution = this . GetService ( typeof ( SVsSolution ) ) as IVsSolution ;
450489
451490 if ( solution != null )
@@ -464,6 +503,8 @@ public int OnAfterCloseSolution(object pUnkReserved)
464503
465504 public int OnAfterOpenSolution ( object pUnkReserved , int fNewSolution )
466505 {
506+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
507+
467508 m_repository . IsSolutionOpen = true ;
468509
469510 var settings = GetSettings ( ) ;
@@ -532,6 +573,7 @@ public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
532573
533574 protected override void Dispose ( bool disposing )
534575 {
576+ Dispatcher . CurrentDispatcher . VerifyAccess ( ) ;
535577 base . Dispose ( disposing ) ;
536578 if ( ! m_isDisposed )
537579 {
0 commit comments