Skip to content

Commit 186e189

Browse files
committed
Switch to InitializeAsync()
If we aren't going to support pre-VS2017 anymore, we can now use the recommended initialize method. This was called out in a warning every time I uploaded a new version of the extension to the marketplace.
1 parent 5997eea commit 186e189

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

OpenFileInSolutionShared/OpenFileInSolutionPackage.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System.Diagnostics;
55
using System.Globalization;
66
using System.Runtime.InteropServices;
7+
using System.Threading;
78
using System.Windows.Interop;
89
using EnvDTE;
910
using Microsoft.VisualStudio.Shell;
11+
using Task = System.Threading.Tasks.Task;
1012

1113
namespace PerniciousGames.OpenFileInSolution
1214
{
@@ -62,13 +64,13 @@ public int GetHashCode(ProjectItemWrapper obj)
6264
/// </summary>
6365
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
6466
// a package.
65-
[PackageRegistration(UseManagedResourcesOnly = true)]
67+
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
6668
// This attribute is used to register the information needed to show this package
6769
// in the Help/About dialog of Visual Studio.
6870
[InstalledProductRegistration("OpenFileInSolution", "Displays a dialog to quick-open any file in the solution", "ID")]
6971
[Guid(GuidList.guidOpenFileInSolutionPkgString)]
7072
[ProvideMenuResource("Menus.ctmenu", 1)]
71-
public sealed class OpenFileInSolutionPackage : Package
73+
public sealed class OpenFileInSolutionPackage : AsyncPackage
7274
{
7375
public abstract class EnvDTEProjectKinds
7476
{
@@ -104,13 +106,17 @@ public OpenFileInSolutionPackage()
104106
/// Initialization of the package; this method is called right after the package is sited, so this is the place
105107
/// where you can put all the initialization code that rely on services provided by VisualStudio.
106108
/// </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)
108113
{
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()));
112118

113-
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
119+
OleMenuCommandService mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
114120
if (null != mcs)
115121
{
116122
// Create the command for the menu item.

0 commit comments

Comments
 (0)