Repository navigation
Expand file tree
/
Copy pathSolutionEventMonitor.cs
More file actions
83 lines (71 loc) · 3.05 KB
/
Copy pathSolutionEventMonitor.cs
File metadata and controls
83 lines (71 loc) · 3.05 KB
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.VisualStudio.Shell;
namespace Microsoft.VisualStudio.LanguageServices
{
/// <summary>
/// Monitors Visual Studio's UIContext for SolutionBuilding and notifies the GlobalOperationService.
/// The intent is to suspend analysis of non-essential files for the duration of a build.
/// </summary>
internal sealed class SolutionEventMonitor : IDisposable
{
private GlobalOperationNotificationService _notificationService;
private Dictionary<string, GlobalOperationRegistration> _operations = new Dictionary<string, GlobalOperationRegistration>();
public SolutionEventMonitor(VisualStudioWorkspace workspace)
{
var notificationService = workspace.Services.GetService<IGlobalOperationNotificationService>() as GlobalOperationNotificationService;
if (notificationService != null)
{
_notificationService = notificationService;
KnownUIContexts.SolutionBuildingContext.UIContextChanged += SolutionBuildingContextChanged;
KnownUIContexts.SolutionOpeningContext.UIContextChanged += SolutionOpeningContextChanged;
}
}
public void Dispose()
{
foreach (var globalOperation in _operations.Values)
{
globalOperation.Dispose();
}
_operations.Clear();
if (_notificationService != null)
{
_notificationService = null;
KnownUIContexts.SolutionBuildingContext.UIContextChanged -= SolutionBuildingContextChanged;
KnownUIContexts.SolutionOpeningContext.UIContextChanged -= SolutionOpeningContextChanged;
}
}
private void SolutionBuildingContextChanged(object sender, UIContextChangedEventArgs e)
{
ContextChangedWorker(e, "Solution Building");
}
private void SolutionOpeningContextChanged(object sender, UIContextChangedEventArgs e)
{
ContextChangedWorker(e, "Solution Opening");
}
private void ContextChangedWorker(UIContextChangedEventArgs e, string operation)
{
if (_notificationService == null)
{
return;
}
TryCancelPendingNotification(operation);
if (e.Activated)
{
_operations[operation] = _notificationService.Start(operation);
}
}
private void TryCancelPendingNotification(string operation)
{
GlobalOperationRegistration globalOperation;
if (_operations.TryGetValue(operation, out globalOperation))
{
globalOperation.Done();
globalOperation.Dispose();
_operations.Remove(operation);
}
}
}
}