3
3
using System . Diagnostics ;
4
4
using System . Globalization ;
5
5
using System . Runtime . InteropServices ;
6
+ using System . Threading ;
7
+ using System . Threading . Tasks ;
6
8
using Microsoft . VisualStudio . Shell ;
7
9
using Microsoft . VisualStudio . Shell . Interop ;
8
10
@@ -14,13 +16,13 @@ namespace Sharomank.RegexTester
14
16
/// The minimum requirement for a class to be considered a valid package for Visual Studio
15
17
/// is to implement the IVsPackage interface and register itself with the shell.
16
18
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
17
- /// to do it: it derives from the Package class that provides the implementation of the
18
- /// IVsPackage interface and uses the registration attributes defined in the framework to
19
+ /// to do it: it derives from the Package class that provides the implementation of the
20
+ /// IVsPackage interface and uses the registration attributes defined in the framework to
19
21
/// register itself and its components with the shell.
20
22
/// </summary>
21
23
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
22
24
// a package.
23
- [ PackageRegistration ( UseManagedResourcesOnly = true ) ]
25
+ [ PackageRegistration ( UseManagedResourcesOnly = true , AllowsBackgroundLoading = true ) ]
24
26
// This attribute is used to register the informations needed to show the this package
25
27
// in the Help/About dialog of Visual Studio.
26
28
[ InstalledProductRegistration ( "#110" , "#112" , "1.0" , IconResourceID = 400 ) ]
@@ -29,13 +31,13 @@ namespace Sharomank.RegexTester
29
31
// This attribute registers a tool window exposed by this package.
30
32
[ ProvideToolWindow ( typeof ( RegexTesterWindow ) ) ]
31
33
[ Guid ( GuidList . guidRegexTesterPkgString ) ]
32
- public sealed class RegexTesterPackage : Package
34
+ public sealed class RegexTesterPackage : AsyncPackage
33
35
{
34
36
/// <summary>
35
37
/// Default constructor of the package.
36
- /// Inside this method you can place any initialization code that does not require
37
- /// any Visual Studio service because at this point the package object is created but
38
- /// not sited yet inside Visual Studio environment. The place to do all the other
38
+ /// Inside this method you can place any initialization code that does not require
39
+ /// any Visual Studio service because at this point the package object is created but
40
+ /// not sited yet inside Visual Studio environment. The place to do all the other
39
41
/// initialization is the Initialize method.
40
42
/// </summary>
41
43
public RegexTesterPackage ( )
@@ -44,12 +46,13 @@ public RegexTesterPackage()
44
46
}
45
47
46
48
/// <summary>
47
- /// This function is called when the user clicks the menu item that shows the
48
- /// tool window. See the Initialize method to see how the menu item is associated to
49
+ /// This function is called when the user clicks the menu item that shows the
50
+ /// tool window. See the Initialize method to see how the menu item is associated to
49
51
/// this function using the OleMenuCommandService service and the MenuCommand class.
50
52
/// </summary>
51
53
private void ShowToolWindow ( object sender , EventArgs e )
52
54
{
55
+ ThreadHelper . ThrowIfNotOnUIThread ( ) ;
53
56
// Get the instance number 0 of this tool window. This window is single instance so this instance
54
57
// is actually the only one.
55
58
// The last flag is set to true so that if the tool window does not exists it will be created.
@@ -71,21 +74,26 @@ private void ShowToolWindow(object sender, EventArgs e)
71
74
/// Initialization of the package; this method is called right after the package is sited, so this is the place
72
75
/// where you can put all the initilaization code that rely on services provided by VisualStudio.
73
76
/// </summary>
74
- protected override void Initialize ( )
77
+ protected async override System . Threading . Tasks . Task InitializeAsync ( CancellationToken cancellationToken , IProgress < ServiceProgressData > progress )
75
78
{
76
- Trace . WriteLine ( string . Format ( CultureInfo . CurrentCulture , "Entering Initialize() of: {0}" , this . ToString ( ) ) ) ;
77
- base . Initialize ( ) ;
79
+ Trace . WriteLine ( string . Format ( CultureInfo . CurrentCulture , "Entering InitializeAsync of: {0}" , this . ToString ( ) ) ) ;
80
+ await base . InitializeAsync ( cancellationToken , progress ) ;
78
81
79
- // Add our command handlers for menu (commands must exist in the .vsct file)
80
- OleMenuCommandService mcs = GetService ( typeof ( IMenuCommandService ) ) as OleMenuCommandService ;
81
- if ( null != mcs )
82
+
83
+ await JoinableTaskFactory . SwitchToMainThreadAsync ( cancellationToken ) ;
84
+
85
+ // Add our command handlers for menu (commands must exist in the .vsct file)
86
+ OleMenuCommandService mcs = await GetServiceAsync ( typeof ( IMenuCommandService ) ) as OleMenuCommandService ;
87
+ if ( null != mcs )
82
88
{
83
89
// Create the command for the tool window
84
90
CommandID toolwndCommandID = new CommandID ( GuidList . guidRegexTesterCmdSet , ( int ) PkgCmdIDList . cmdidRegexTester ) ;
85
91
MenuCommand menuToolWin = new MenuCommand ( ShowToolWindow , toolwndCommandID ) ;
86
- mcs . AddCommand ( menuToolWin ) ;
92
+ mcs . AddCommand ( menuToolWin ) ;
87
93
}
94
+
88
95
}
96
+
89
97
#endregion
90
98
91
99
}
0 commit comments