Skip to content

Commit e417ad0

Browse files
committed
Use AsyncPackage instead of Package
1 parent 0e70fe8 commit e417ad0

File tree

7 files changed

+60
-81
lines changed

7 files changed

+60
-81
lines changed

solution/GraphicalDebugging/Debugger.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using EnvDTE;
2-
using Microsoft.VisualStudio.Debugger.ComponentInterfaces;
3-
using System;
4-
using System.Windows.Forms;
5-
using static GraphicalDebugging.MemoryReader;
2+
using Microsoft.VisualStudio.Shell;
63

74
namespace GraphicalDebugging
85
{

solution/GraphicalDebugging/ExpressionLoader.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
using Microsoft.VisualStudio.Shell;
1010

1111
using System;
12-
using System.Collections;
1312
using System.Collections.Generic;
14-
using System.Globalization;
15-
using System.Threading;
13+
using System.Threading.Tasks;
1614

1715
namespace GraphicalDebugging
1816
{
@@ -57,11 +55,13 @@ public static bool IsBreakMode
5755
}
5856

5957
private static ExpressionLoader Instance { get; set; }
60-
61-
public static void Initialize(GraphicalDebuggingPackage package)
58+
59+
public static async Task InitializeAsync(GraphicalDebuggingPackage package)
6260
{
63-
DTE2 dte = package.GetService(typeof(DTE)) as DTE2;
64-
61+
DTE2 dte = await package.GetServiceAsync(typeof(DTE)) as DTE2;
62+
63+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
64+
6565
Instance = new ExpressionLoader(dte);
6666
}
6767

solution/GraphicalDebugging/GeometryWatchCommand.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,23 @@ internal sealed class GeometryWatchCommand
3131
/// <summary>
3232
/// VS Package that provides this command, not null.
3333
/// </summary>
34-
private readonly Package package;
34+
private readonly AsyncPackage package;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="GeometryWatchCommand"/> class.
3838
/// Adds our command handlers for menu (commands must exist in the command table file)
3939
/// </summary>
4040
/// <param name="package">Owner package, not null.</param>
41-
private GeometryWatchCommand(Package package, OleMenuCommandService commandService)
41+
private GeometryWatchCommand(AsyncPackage package, OleMenuCommandService commandService)
4242
{
4343
ThreadHelper.ThrowIfNotOnUIThread();
4444

45-
if (package == null)
46-
{
47-
throw new ArgumentNullException("package");
48-
}
45+
this.package = package ?? throw new ArgumentNullException(nameof(package));
46+
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
4947

50-
this.package = package;
51-
52-
if (commandService != null)
53-
{
54-
var menuCommandID = new CommandID(CommandSet, CommandId);
55-
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
56-
commandService.AddCommand(menuItem);
57-
}
48+
var menuCommandID = new CommandID(CommandSet, CommandId);
49+
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
50+
commandService.AddCommand(menuItem);
5851
}
5952

6053
/// <summary>
@@ -69,7 +62,7 @@ public static GeometryWatchCommand Instance
6962
/// <summary>
7063
/// Gets the service provider from the owner package.
7164
/// </summary>
72-
private IServiceProvider ServiceProvider
65+
private IAsyncServiceProvider ServiceProvider
7366
{
7467
get
7568
{
@@ -81,10 +74,12 @@ private IServiceProvider ServiceProvider
8174
/// Initializes the singleton instance of the command.
8275
/// </summary>
8376
/// <param name="package">Owner package, not null.</param>
84-
public static void Initialize(GraphicalDebuggingPackage package)
77+
public static async Task InitializeAsync(GraphicalDebuggingPackage package)
8578
{
86-
OleMenuCommandService commandService = package.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
87-
79+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
80+
81+
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
82+
8883
Instance = new GeometryWatchCommand(package, commandService);
8984
}
9085

solution/GraphicalDebugging/GraphicalDebuggingPackage.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace GraphicalDebugging
3030
/// To get loaded into VS, the package must be referred by &lt;Asset Type="Microsoft.VisualStudio.VsPackage" ...&gt; in .vsixmanifest file.
3131
/// </para>
3232
/// </remarks>
33-
[PackageRegistration(UseManagedResourcesOnly = true)]
33+
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
3434
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
3535
[ProvideMenuResource("Menus.ctmenu", 1)]
3636
[Guid(GraphicalDebuggingPackage.PackageGuidString)]
@@ -42,7 +42,7 @@ namespace GraphicalDebugging
4242
[ProvideOptionPage(typeof(GeometryWatchOptionPage), "Graphical Debugging", "Geometry Watch", 0, 0, true)]
4343
[ProvideOptionPage(typeof(GraphicalWatchOptionPage), "Graphical Debugging", "Graphical Watch", 0, 0, true)]
4444
[ProvideOptionPage(typeof(PlotWatchOptionPage), "Graphical Debugging", "Plot Watch", 0, 0, true)]
45-
public sealed class GraphicalDebuggingPackage : Package
45+
public sealed class GraphicalDebuggingPackage : AsyncPackage
4646
{
4747
/// <summary>
4848
/// GraphicalDebuggingPackage GUID string.
@@ -71,22 +71,19 @@ public GraphicalDebuggingPackage()
7171
/// Initialization of the package; this method is called right after the package is sited, so this is the place
7272
/// where you can put all the initialization code that rely on services provided by VisualStudio.
7373
/// </summary>
74-
protected override void Initialize()
74+
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
7575
{
76-
Instance = this;
76+
// When initialized asynchronously, the current thread may be a background thread at this point.
77+
// Do any initialization that requires the UI thread after switching to the UI thread.
78+
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
7779

78-
base.Initialize();
80+
Instance = this;
7981

80-
ExpressionLoader.Initialize(this);
82+
await ExpressionLoader.InitializeAsync(this);
8183

82-
GeometryWatchCommand.Initialize(this);
83-
GraphicalWatchCommand.Initialize(this);
84-
PlotWatchCommand.Initialize(this);
85-
}
86-
87-
public new object GetService(Type serviceType)
88-
{
89-
return base.GetService(serviceType);
84+
await GeometryWatchCommand.InitializeAsync(this);
85+
await GraphicalWatchCommand.InitializeAsync(this);
86+
await PlotWatchCommand.InitializeAsync(this);
9087
}
9188

9289
public new DialogPage GetDialogPage(Type dialogPageType)

solution/GraphicalDebugging/GraphicalWatchCommand.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,23 @@ internal sealed class GraphicalWatchCommand
3131
/// <summary>
3232
/// VS Package that provides this command, not null.
3333
/// </summary>
34-
private readonly Package package;
34+
private readonly AsyncPackage package;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="GraphicalWatchCommand"/> class.
3838
/// Adds our command handlers for menu (commands must exist in the command table file)
3939
/// </summary>
4040
/// <param name="package">Owner package, not null.</param>
41-
private GraphicalWatchCommand(Package package, OleMenuCommandService commandService)
41+
private GraphicalWatchCommand(AsyncPackage package, OleMenuCommandService commandService)
4242
{
4343
ThreadHelper.ThrowIfNotOnUIThread();
4444

45-
if (package == null)
46-
{
47-
throw new ArgumentNullException("package");
48-
}
49-
50-
this.package = package;
45+
this.package = package ?? throw new ArgumentNullException(nameof(package));
46+
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
5147

52-
if (commandService != null)
53-
{
54-
var menuCommandID = new CommandID(CommandSet, CommandId);
55-
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
56-
commandService.AddCommand(menuItem);
57-
}
48+
var menuCommandID = new CommandID(CommandSet, CommandId);
49+
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
50+
commandService.AddCommand(menuItem);
5851
}
5952

6053
/// <summary>
@@ -69,7 +62,7 @@ public static GraphicalWatchCommand Instance
6962
/// <summary>
7063
/// Gets the service provider from the owner package.
7164
/// </summary>
72-
private IServiceProvider ServiceProvider
65+
private IAsyncServiceProvider ServiceProvider
7366
{
7467
get
7568
{
@@ -81,9 +74,11 @@ private IServiceProvider ServiceProvider
8174
/// Initializes the singleton instance of the command.
8275
/// </summary>
8376
/// <param name="package">Owner package, not null.</param>
84-
public static void Initialize(GraphicalDebuggingPackage package)
77+
public static async Task InitializeAsync(GraphicalDebuggingPackage package)
8578
{
86-
OleMenuCommandService commandService = package.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
79+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
80+
81+
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
8782

8883
Instance = new GraphicalWatchCommand(package, commandService);
8984
}

solution/GraphicalDebugging/PlotWatchCommand.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,23 @@ internal sealed class PlotWatchCommand
3131
/// <summary>
3232
/// VS Package that provides this command, not null.
3333
/// </summary>
34-
private readonly Package package;
34+
private readonly AsyncPackage package;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="PlotWatchCommand"/> class.
3838
/// Adds our command handlers for menu (commands must exist in the command table file)
3939
/// </summary>
4040
/// <param name="package">Owner package, not null.</param>
41-
private PlotWatchCommand(Package package, OleMenuCommandService commandService)
41+
private PlotWatchCommand(AsyncPackage package, OleMenuCommandService commandService)
4242
{
4343
ThreadHelper.ThrowIfNotOnUIThread();
4444

45-
if (package == null)
46-
{
47-
throw new ArgumentNullException("package");
48-
}
49-
50-
this.package = package;
45+
this.package = package ?? throw new ArgumentNullException(nameof(package));
46+
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
5147

52-
if (commandService != null)
53-
{
54-
var menuCommandID = new CommandID(CommandSet, CommandId);
55-
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
56-
commandService.AddCommand(menuItem);
57-
}
48+
var menuCommandID = new CommandID(CommandSet, CommandId);
49+
var menuItem = new MenuCommand(this.ShowToolWindow, menuCommandID);
50+
commandService.AddCommand(menuItem);
5851
}
5952

6053
/// <summary>
@@ -69,7 +62,7 @@ public static PlotWatchCommand Instance
6962
/// <summary>
7063
/// Gets the service provider from the owner package.
7164
/// </summary>
72-
private IServiceProvider ServiceProvider
65+
private IAsyncServiceProvider ServiceProvider
7366
{
7467
get
7568
{
@@ -81,9 +74,11 @@ private IServiceProvider ServiceProvider
8174
/// Initializes the singleton instance of the command.
8275
/// </summary>
8376
/// <param name="package">Owner package, not null.</param>
84-
public static void Initialize(GraphicalDebuggingPackage package)
77+
public static async Task InitializeAsync(GraphicalDebuggingPackage package)
8578
{
86-
OleMenuCommandService commandService = package.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
79+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
80+
81+
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
8782

8883
Instance = new PlotWatchCommand(package, commandService);
8984
}

solution/GraphicalDebugging/Util.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
using System.Windows.Interop;
2020
using System.Windows.Media;
2121
using System.Windows.Media.Imaging;
22-
using System.Threading;
23-
using Task = System.Threading.Tasks.Task;
24-
using Microsoft.VisualStudio.OLE.Interop;
22+
2523

2624
namespace GraphicalDebugging
2725
{
@@ -240,9 +238,11 @@ public static string Tparam(string type, int index)
240238
: "";
241239
}
242240

243-
public static void ShowWindow<WatchWindow>(Package package, string name)
241+
public static void ShowWindow<WatchWindow>(AsyncPackage package, string name)
244242
where WatchWindow : ToolWindowPane
245243
{
244+
ThreadHelper.ThrowIfNotOnUIThread();
245+
246246
for (int i = 0; i < 10; ++i)
247247
{
248248
ToolWindowPane window = package.FindToolWindow(typeof(WatchWindow), i, false);

0 commit comments

Comments
 (0)