|
4 | 4 | using System.Diagnostics; |
5 | 5 | using System.Globalization; |
6 | 6 | using System.Runtime.InteropServices; |
| 7 | +using System.Threading; |
7 | 8 | using System.Windows.Interop; |
8 | 9 | using EnvDTE; |
9 | 10 | using Microsoft.VisualStudio.Shell; |
| 11 | +using Task = System.Threading.Tasks.Task; |
10 | 12 |
|
11 | 13 | namespace PerniciousGames.OpenFileInSolution |
12 | 14 | { |
@@ -62,13 +64,13 @@ public int GetHashCode(ProjectItemWrapper obj) |
62 | 64 | /// </summary> |
63 | 65 | // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is |
64 | 66 | // a package. |
65 | | - [PackageRegistration(UseManagedResourcesOnly = true)] |
| 67 | + [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] |
66 | 68 | // This attribute is used to register the information needed to show this package |
67 | 69 | // in the Help/About dialog of Visual Studio. |
68 | 70 | [InstalledProductRegistration("OpenFileInSolution", "Displays a dialog to quick-open any file in the solution", "ID")] |
69 | 71 | [Guid(GuidList.guidOpenFileInSolutionPkgString)] |
70 | 72 | [ProvideMenuResource("Menus.ctmenu", 1)] |
71 | | - public sealed class OpenFileInSolutionPackage : Package |
| 73 | + public sealed class OpenFileInSolutionPackage : AsyncPackage |
72 | 74 | { |
73 | 75 | public abstract class EnvDTEProjectKinds |
74 | 76 | { |
@@ -104,13 +106,17 @@ public OpenFileInSolutionPackage() |
104 | 106 | /// Initialization of the package; this method is called right after the package is sited, so this is the place |
105 | 107 | /// where you can put all the initialization code that rely on services provided by VisualStudio. |
106 | 108 | /// </summary> |
107 | | - protected override void Initialize() |
| 109 | + /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param> |
| 110 | + /// <param name="progress">A provider for progress updates.</param> |
| 111 | + /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns> |
| 112 | + protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress) |
108 | 113 | { |
109 | | - ThreadHelper.ThrowIfNotOnUIThread(); |
110 | | - Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); |
111 | | - base.Initialize(); |
| 114 | + // When initialized asynchronously, the current thread may be a background thread at this point. |
| 115 | + // Do any initialization that requires the UI thread after switching to the UI thread. |
| 116 | + await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); |
| 117 | + Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering InitializeAsync() of: {0}", this.ToString())); |
112 | 118 |
|
113 | | - OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; |
| 119 | + OleMenuCommandService mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; |
114 | 120 | if (null != mcs) |
115 | 121 | { |
116 | 122 | // Create the command for the menu item. |
|
0 commit comments