-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VSPackage.cs
51 lines (44 loc) · 2.08 KB
/
VSPackage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Events;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
namespace SolutionLoadSample
{
[Guid("61eadc52-5677-4548-b273-08f1e6574f71")]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("Solution Load Sample", "Demonstrates use of solution load events", "1.0")]
// The following line will schedule the package to be initialized when a solution is being opened
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class VSPackage : AsyncPackage
{
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Since this package might not be initialized until after a solution has finished loading,
// we need to check if a solution has already been loaded and then handle it.
bool isSolutionLoaded = await IsSolutionLoadedAsync();
if (isSolutionLoaded)
{
HandleOpenSolution();
}
// Listen for subsequent solution events
SolutionEvents.OnAfterOpenSolution += HandleOpenSolution;
}
private async Task<bool> IsSolutionLoadedAsync()
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
var solService = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution;
ErrorHandler.ThrowOnFailure(solService.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object value));
return value is bool isSolOpen && isSolOpen;
}
private void HandleOpenSolution(object sender = null, EventArgs e = null)
{
// Handle the open solution and try to do as much work
// on a background thread as possible
}
}
}