aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/QtVsToolsPackage.cs
blob: 735f2211f1d99bb29f4ce3cab8d8c91ab680ab78 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TaskStatusCenter;
using Microsoft.VisualStudio.Threading;
using Microsoft.Win32;

using static Microsoft.VisualStudio.Shell.PackageAutoLoadFlags;
using Task = System.Threading.Tasks.Task;

namespace QtVsTools
{
    using Core;
    using Package;
    using Package.CMake;
    using Qml.Debug;
    using QtVsTools.Core.Common;
    using VisualStudio;

    public static partial class Instances
    {
        public static QtVsToolsPackage Package => QtVsToolsPackage.Instance;
    }

    [Guid(QtMenus.Package.GuidString)]
    [InstalledProductRegistration(Vsix.Name, Vsix.Description, Version.PRODUCT_VERSION)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [ProvideAutoLoad(UIContextGuids.SolutionExists, BackgroundLoad)]
    [ProvideAutoLoad(UIContextGuids.NoSolution, BackgroundLoad)]
    [ProvideAutoLoad(UIContextGuids.EmptySolution, BackgroundLoad)]
    [ProvideAutoLoad(UIContextGuids.SolutionHasSingleProject, BackgroundLoad)]
    [ProvideAutoLoad(UIContextGuids.SolutionHasMultipleProjects, BackgroundLoad)]
    [ProvideAutoLoad(UIContextGuids.CodeWindow, BackgroundLoad)]

    [ProvideEditorExtension(typeof(Package.MsBuild.ConversionReportViewer),
        extension: ".qtvscr",
        priority: 999,
        DefaultName = "Qt/MSBuild Project Format Conversion Report")]
    [ProvideEditorLogicalView(typeof(Package.MsBuild.ConversionReportViewer),
        logicalViewGuid: VSConstants.LOGVIEWID.TextView_string)]

    // Custom editor: Qt Designer
    [ProvideEditorExtension(typeof(Package.Editors.QtDesigner),
        extension: ".ui",
        priority: 999,
        DefaultName = Package.Editors.QtDesigner.Title)]
    [ProvideEditorLogicalView(typeof(Package.Editors.QtDesigner),
        logicalViewGuid: VSConstants.LOGVIEWID.TextView_string)]

    // Custom editor: Qt Linguist
    [ProvideEditorExtension(typeof(Package.Editors.QtLinguist),
        extension: ".ts",
        priority: 999,
        DefaultName = Package.Editors.QtLinguist.Title)]
    [ProvideEditorLogicalView(typeof(Package.Editors.QtLinguist),
        logicalViewGuid: VSConstants.LOGVIEWID.TextView_string)]

    // Custom editor: Qt Resource Editor
    [ProvideEditorExtension(typeof(Package.Editors.QtResourceEditor),
        extension: ".qrc",
        priority: 999,
        DefaultName = Package.Editors.QtResourceEditor.Title)]
    [ProvideEditorLogicalView(typeof(Package.Editors.QtResourceEditor),
        logicalViewGuid: VSConstants.LOGVIEWID.TextView_string)]

    // Options page
    [ProvideOptionPage(typeof(Core.Options.QtOptionsPage),
        "Qt", "General", 0, 0, true, Sort = 0)]

    // Test Adapter page
    [ProvideOptionPage(typeof(TestAdapter.QtTestPage),
        "Qt", "Test Adapter", 0, 0, true, Sort = 1)]

    // Qt Versions page
    [ProvideOptionPage(typeof(Core.Options.QtVersionsPage),
        "Qt", "Versions", 0, 0, true, Sort = 2)]

    [ProvideLaunchHook(typeof(QmlDebugLaunchHook))]

    [ProvideService((typeof(SIdleTaskManager)), IsAsyncQueryable = true)]

    public sealed class QtVsToolsPackage : AsyncPackage, IVsServiceProvider
    {
        public DTE Dte { get; private set; }

        public Package.Editors.QtDesigner QtDesigner { get; private set; }
        public Package.Editors.QtLinguist QtLinguist { get; private set; }
        private Package.Editors.QtResourceEditor QtResourceEditor { get; set; }

        public static EventWaitHandle Initialized { get; } = new(false, EventResetMode.ManualReset);
        private static bool InitializationAwaited { get; set; } = false;

        public static QtVsToolsPackage Instance { get; private set; }

        private DteEventsHandler EventHandler { get; set; }

        private Guid legacyPackageId = new("6E7FA583-5FAA-4EC9-9E90-4A0AE5FD61EE");
        private const string LegacyPackageName = "QtVsToolsLegacyPackage";

        private ConcurrentStopwatch InitTimer { get; set; }
        private ConcurrentStopwatch UiTimer { get; set; }

        private uint debuggerEventsCookie;
        private DebuggerEvents debuggerEventsHandler;

        private uint buildEventsCookie;
        private UpdateSolutionEvents buildEventsHandler;

        protected override async Task InitializeAsync(
            CancellationToken cancellationToken,
            IProgress<ServiceProgressData> progress)
        {
            try {
                InitTimer = ConcurrentStopwatch.StartNew();

                VsServiceProvider.Instance = Instance = this;
                AddService(typeof(SIdleTaskManager), CreateServiceAsync);

                var packages = await GetServiceAsync<
                    SVsPackageInfoQueryService, IVsPackageInfoQueryService>();

                ///////////////////////////////////////////////////////////////////////////////////
                // Switch to main (UI) thread
                await JoinableTaskFactory.SwitchToMainThreadAsync();
                UiTimer = ConcurrentStopwatch.StartNew();

                if (packages?.GetPackageInfo(ref legacyPackageId) is { Name: LegacyPackageName } )
                    throw new InvalidOperationException("Legacy extension detected.");

                if ((Dte = await VsServiceProvider.GetServiceAsync<DTE>()) == null)
                    throw new InvalidOperationException("Unable to get service: DTE");

                if (await VsServiceProvider.GetServiceAsync<IVsAppCommandLine>() is {} cmd) {
                    var result = cmd.GetOption("rootSuffix", out var exists, out var suffix);
                    if (result == VSConstants.S_OK && exists > 0)
                        Resources.RegistrySuffix = @"\" + suffix;
                } else {
                    Messages.Print("Unable to get service: IVsAppCommandLine");
                }

                if (Dte.CommandLineArguments?.Contains("/Command QtVSTools.ClearSettings") == true) {
                    Registry.CurrentUser.DeleteSubKeyTree(Resources.ObsoleteRegistryPath, false);
                    Registry.CurrentUser.DeleteSubKeyTree(Resources.RegistryPath, false);
                }

                if (await VsServiceProvider.GetServiceAsync<IVsDebugger>() is {} service) {
                    debuggerEventsHandler = new DebuggerEvents(Dte);
                    service.AdviseDebuggerEvents(debuggerEventsHandler, out debuggerEventsCookie);
                }

                // TODO: At some point, maybe version 4.0 or later, we can remove this call again.
                Utils.SetRegistryKeyOnce(Resources.SettingsRegistryPath, "QmlLsp_Enable", 1,
                    RegistryValueKind.DWord, "QmlLsp_Enable_Changed");

                Qml.Debug.Launcher.Initialize();
                QtMainMenu.Initialize();
                AddCMakeItem.Initialize();
                QtSolutionContextMenu.Initialize();
                QtProjectContextMenu.Initialize();
                QtItemContextMenu.Initialize();
                RegisterEditorFactory(QtDesigner = new Package.Editors.QtDesigner());
                RegisterEditorFactory(QtLinguist = new Package.Editors.QtLinguist());
                RegisterEditorFactory(QtResourceEditor = new Package.Editors.QtResourceEditor());
                RegisterEditorFactory(new Package.MsBuild.ConversionReportViewer());
                QtHelp.Initialize();

                if (!string.IsNullOrEmpty(VsShell.InstallRootDir))
                    QMakeImport.VcPath = Path.Combine(VsShell.InstallRootDir, "VC");

                ///////////////////////////////////////////////////////////////////////////////////
                // Switch to background thread
                await TaskScheduler.Default;
                UiTimer.Stop();

                ///////////
                // Install Qt/MSBuild files from package folder to standard location
                //  -> %LOCALAPPDATA%\QtMsBuild
                //
                var qtMsBuildDefault = Path.Combine(
                    Environment.GetEnvironmentVariable("LocalAppData") ?? "", "QtMsBuild");
                try {
                    var qtMsBuildDefaultUri = new Uri(qtMsBuildDefault + Path.DirectorySeparatorChar);
                    var qtMsBuildVsixPath = Path.Combine(Utils.PackageInstallPath, "QtMsBuild");
                    var qtMsBuildVsixUri = new Uri(qtMsBuildVsixPath + Path.DirectorySeparatorChar);
                    if (qtMsBuildVsixUri != qtMsBuildDefaultUri) {
                        var qtMsBuildVsixFiles = Directory
                            .GetFiles(qtMsBuildVsixPath, "*", SearchOption.AllDirectories)
                            .Select(x => qtMsBuildVsixUri.MakeRelativeUri(new Uri(x)));
                        foreach (var qtMsBuildFile in qtMsBuildVsixFiles) {
                            var sourcePath = new Uri(qtMsBuildVsixUri, qtMsBuildFile).LocalPath;
                            var targetPath = new Uri(qtMsBuildDefaultUri, qtMsBuildFile).LocalPath;
                            var targetPathTemp = targetPath + ".tmp";
                            Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? "");
                            File.Copy(sourcePath, targetPathTemp, overwrite: true);
                            ////////
                            // Copy Qt/MSBuild files to standard location, taking care not to
                            // overwrite the updated Qt props file, possibly containing user-defined
                            // build settings (written by the VS Property Manager). This file is
                            // recognized as being named "Qt.props" and containing the import
                            // statement for qt_private.props.
                            //
                            const string qtPrivateImport
                                = @"<Import Project=""$(MSBuildThisFileDirectory)\qt_private.props""";
                            static bool IsUpdateQtProps(string path) =>
                                string.Equals(Path.GetFileName(path), "Qt.props", Utils.IgnoreCase)
                                    && File.ReadAllText(path).Contains(qtPrivateImport);

                            if (!File.Exists(targetPath)) {
                                // Target file does not exist
                                //  -> Create new
                                File.Move(targetPathTemp, targetPath);
                            } else if (!IsUpdateQtProps(targetPath)) {
                                // Target file is not the updated Qt.props
                                //  -> Overwrite
                                File.Replace(targetPathTemp, targetPath, null);
                            } else {
                                // Target file *is* the updated Qt.props; skip!
                                //  -> Remove temp file
                                Utils.DeleteFile(targetPathTemp);
                            }
                        }
                    }
                } catch {
                    /////////
                    // Error copying files to standard location.
                    //  -> FAIL-SAFE: use source folder (within package) as the standard location
                    qtMsBuildDefault = Path.Combine(Utils.PackageInstallPath, "QtMsBuild");
                }

                ///////
                // Set %QTMSBUILD% by default to point to standard location of Qt/MSBuild
                //
                var qtMsBuildPath = Environment.GetEnvironmentVariable("QtMsBuild");
                if (string.IsNullOrEmpty(qtMsBuildPath)) {

                    Environment.SetEnvironmentVariable(
                        "QtMsBuild", qtMsBuildDefault,
                        EnvironmentVariableTarget.User);

                    Environment.SetEnvironmentVariable(
                        "QtMsBuild", qtMsBuildDefault,
                        EnvironmentVariableTarget.Process);
                }

                CopyTextMateLanguageFiles();
                InitTimer.Stop();

            } catch (Exception ex) {
                var properties = new Dictionary<string, string>
                {
                    {"Operation", GetType().FullName + ".InitializeAsync"}
                };
                Telemetry.TrackException(ex, properties);
                var activityLog = await GetServiceAsync<SVsActivityLog, IVsActivityLog>();
                activityLog?.LogEntry((uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, ToString(),
                    "Failed to load QtVsTools package. Exception details:\n"
                        + $" Message: {ex.Message}\n"
                        + $" Source: {ex.Source}\n"
                        + $" Stack Trace: {ex.StackTrace}\n"
                        + $" Target Site: {ex.TargetSite}\n"
                        + (ex.InnerException != null
                            ? $"Inner Exception Message: {ex.InnerException.Message}\n"
                                + $" Inner Exception Stack Trace: {ex.InnerException.StackTrace}\n"
                            : "")
                );
                throw; // VS will catch the exception and mark the extension as failed to load.
            }
        }

        protected override async Task OnAfterPackageLoadedAsync(CancellationToken cancellationToken)
        {
            await Task.WhenAll(
                /////////
                // Move registered Qt versions
                //
                Task.Run(QtVersionManager.MoveRegisteredQtVersions, cancellationToken),

                /////////
                // Initialize Qt versions information
                //
                CheckVersionsAsync(),

                /////////
                // Copy natvis files
                //
                NatvisHelper.CopyVisualizersFilesAsync(),

                /////////
                // Setup QTDIR environment variable
                //
                Task.Run(() =>
                {
                    if (QtVersionManager.GetInstallPath("$(DefaultQtVersion)") is not {} path)
                        return;
                    if (new[] { "SSH:", "WSL:" }.Any(path.StartsWith))
                        return;
                    if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("QTDIR"))) {
                        Environment.SetEnvironmentVariable("QTDIR", path,
                            EnvironmentVariableTarget.Process);
                    }
                }, cancellationToken),

                /////////
                // Force download and install of local QML Language Server, otherwise it's
                // periodically checked if Visual Studio is idling.
                //
                RunQmlLanguageServerMonitorTaskOnceAsync(cancellationToken)
            );


            /////////
            // Show banner
            //
            Messages.Print(trim: false, text: @$"

    ################################################################
        == Qt Visual Studio Tools version {Version.USER_VERSION} ==
            Extension package initialized in:
             * Total: {InitTimer.Elapsed.TotalMilliseconds:0.##} ms
             * UI thread: {UiTimer.Elapsed.TotalMilliseconds:0.##} ms
    ################################################################");

            /////////
            // Switch to main (UI) thread
            //
            await JoinableTaskFactory.SwitchToMainThreadAsync();

            /////////
            // Send telemetry
            //
            var properties = new Dictionary<string, string>
            {
                {"VSVersion", Dte.Version},
                {"VSToolsVersion", Version.PRODUCT_VERSION},
                {"QtVersions", string.Join(";", QtVersionManager.GetVersions())}
            };
            var metrics = new Dictionary<string, double>
            {
                {"InitTimer", InitTimer.Elapsed.TotalMilliseconds},
                {"UiTimer", UiTimer.Elapsed.TotalMilliseconds}
            };
            Telemetry.TrackEvent(GetType().FullName + ".OnAfterPackageLoadedAsync", properties,
                metrics);

            /////////
            // Initialize DTE event handlers.
            //
            EventHandler = new DteEventsHandler(Dte);

            /////////
            // Wire up solution change events. Currently, targets project configuration
            // changed events only. Used in conjunction with the QML Language Server.
            //
            var manager = await VsServiceProvider.GetServiceAsync<IVsSolutionBuildManager>();
            if (manager != null) {
                buildEventsHandler = new UpdateSolutionEvents();
                manager.AdviseUpdateSolutionEvents(buildEventsHandler, out buildEventsCookie);
            }

            /////////
            // Check if a solution was opened during initialization.
            // If so, fire solution open event.
            //
            if (VsShell.FolderWorkspace?.CurrentWorkspace is not null)
                EventHandler.OnActiveWorkspaceChanged();
            else if (Dte.Solution?.IsOpen == true)
                EventHandler.SolutionEvents_Opened();

            if (Dte.Debugger.CurrentMode != dbgDebugMode.dbgDesignMode) {
                foreach (EnvDTE.Process proc in Dte.Debugger.DebuggedProcesses) {
                    if (Qml.Debug.Launcher.TryAttachToProcess((uint)proc.ProcessID))
                        break;
                }
            }

            /////////
            // Enable output messages and activate output pane.
            //
            Messages.Initialized = true;
            Messages.ActivateMessagePane();


            if (await GetServiceAsync<SIdleTaskManager, IIdleTaskManager>() is {} service) {
                service.Add(new DevReleaseMonitorTask());
                service.Add(new QmlLanguageServerMonitorTask());
            }

            /////////
            // Signal package initialization complete.
            //
            Initialized.Set();

            await base.OnAfterPackageLoadedAsync(cancellationToken);
        }

        public static async Task WaitUntilInitializedAsync()
        {
            InitializationAwaited = true;
            await Initialized;
        }

        public static bool IsInitialized => Initialized.WaitOne(0);

        private async Task CheckVersionsAsync()
        {
            await VsShell.UiThreadAsync(() =>
                StatusBar.SetText("Checking installed Qt versions..."));

            var defaultPath = QtVersionManager.GetDefaultVersionInstallPath();
            Messages.Print($"--- Checking default Qt version...{Environment.NewLine}"
                + (QtPaths.Exists(defaultPath) || QMake.Exists(defaultPath)
                  ? "--- default Qt version check OK" : "--> default Qt version missing."));

            var versions = QtVersionManager.GetVersions();
            var statusCenter = await VsServiceProvider
                .GetServiceAsync<SVsTaskStatusCenterService, IVsTaskStatusCenterService>();
            var status = statusCenter?.PreRegister(
                    new TaskHandlerOptions
                    {
                        Title = "Qt VS Tools: Checking installed Qt versions..."
                    },
                    new TaskProgressData
                    {
                        ProgressText = $"{versions.Length} version(s)",
                        CanBeCanceled = false,
                        PercentComplete = 0
                    })
                    as ITaskHandler2;
            status?.RegisterTask(new (() => throw new InvalidOperationException()));
            status?.Progress.Report(new TaskProgressData
            {
                ProgressText = $"{versions.Length} version(s)",
                CanBeCanceled = false,
                PercentComplete = 0
            });

            var tasks = versions.Select((version, idx) => JoinableTaskFactory.RunAsync(async () =>
            {
                await Task.Yield();
                Messages.Print($@"
--- Checking {version} ...");
                var timer = Stopwatch.StartNew();
                var qt = VersionInformation.GetOrAddByName(version);
                if (Directory.Exists(qt?.InstallPrefix ?? string.Empty)) {
                    Messages.Print($@"
--- {version} check OK ({timer.Elapsed.TotalSeconds:0.##} secs)");
                } else {
                    Messages.Print($@"
--> {version} Missing or cross-platform installation; skipped.");
                }
                if (InitializationAwaited) {
                    await VsShell.UiThreadAsync(() => StatusBar.Progress(
                        $"Checking Qt version: {version}", versions.Length, idx));
                }
                status?.Progress.Report(new TaskProgressData
                {
                    ProgressText = $"{version} ({versions.Length - idx - 1} remaining)",
                    CanBeCanceled = false,
                    PercentComplete = (100 * (idx + 1)) / versions.Length
                });
            }).Task);

            await Task.WhenAll(tasks);

            if (InitializationAwaited)
                await VsShell.UiThreadAsync(StatusBar.ResetProgress);
            await VsShell.UiThreadAsync(StatusBar.Clear);
            status?.Dismiss();
        }

        protected override int QueryClose(out bool canClose)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            Telemetry.Flush();
            EventHandler?.Disconnect();
            return base.QueryClose(out canClose);
        }

        private void CopyTextMateLanguageFiles()
        {
            var qtTmLanguagePath = Environment.
                ExpandEnvironmentVariables("%USERPROFILE%\\.vs\\Extensions\\qttmlanguage");

            // always copy .pri/.pro TextMate Language Grammar file
            Utils.CopyDirectory(Path.Combine(Utils.PackageInstallPath, "qttmlanguage"),
                qtTmLanguagePath);

            //Remove TextMate-based QML syntax highlighting
            Utils.DeleteDirectory(Path.Combine(qtTmLanguagePath, "qml"), Utils.Option.Recursive);
        }

        public T2 GetService<T1, T2>()
            where T1 : class
            where T2 : class
        {
            return GetService(typeof(T1)) as T2;
        }

        public async Task<T2> GetServiceAsync<T1, T2>()
            where T1 : class
            where T2 : class
        {
            return await GetServiceAsync(typeof(T1)) as T2;
        }

        private SIdleTaskManager idleTaskManager;
        private async Task<object> CreateServiceAsync(IAsyncServiceContainer container,
            CancellationToken cancellationToken, Type serviceType)
        {
            if (container != this || serviceType != typeof(SIdleTaskManager))
                return null;

            if (idleTaskManager != null)
                return idleTaskManager;

            var service = new IdleTaskManager(ThreadHelper.JoinableTaskContext);
            await service.InitializeAsync(this, cancellationToken);

            return idleTaskManager ??= service;
        }

        private static async Task RunQmlLanguageServerMonitorTaskOnceAsync(CancellationToken token)
        {
            if (Directory.Exists(QmlLanguageServerManager.InstallDir))
                return;
            try {
                await new QmlLanguageServerMonitorTask().RunAsync(token);
            } catch {
                Utils.DeleteDirectory(QmlLanguageServerManager.InstallDir, Utils.Option.Recursive);
            }
        }

        protected override void Dispose(bool disposing)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (disposing) {
                var debugger = GetService<IVsDebugger, IVsDebugger>();
                if (debugger != null && debuggerEventsCookie != 0) {
                    debugger.UnadviseDebuggerEvents(debuggerEventsCookie);
                    debuggerEventsCookie = 0;
                }

                var buildManager = GetService<SVsSolutionBuildManager, IVsSolutionBuildManager>();
                if (buildManager != null && buildEventsCookie != 0) {
                    buildManager.UnadviseUpdateSolutionEvents(buildEventsCookie);
                    buildEventsCookie = 0;
                }
            }
            base.Dispose(disposing);
        }
    }
}