|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
3 | 4 | using System.IO; |
4 | 5 | using System.Threading; |
5 | 6 | using System.Threading.Tasks; |
@@ -79,20 +80,39 @@ public Watcher(IRepository repo, string fullpath, string gitDir) |
79 | 80 | } |
80 | 81 |
|
81 | 82 | _timer = new Timer(Tick, null, 100, 100); |
| 83 | + ViewModels.Preferences.Instance.PropertyChanged += OnPreferencesChanged; |
82 | 84 |
|
83 | 85 | // Starts filesystem watchers in another thread to avoid UI blocking |
84 | | - Task.Run(() => |
| 86 | + Task.Run(UpdateWatching); |
| 87 | + } |
| 88 | + |
| 89 | + private void UpdateWatching() |
| 90 | + { |
| 91 | + lock (_syncRoot) |
85 | 92 | { |
| 93 | + if (_disposed) |
| 94 | + return; |
| 95 | + |
86 | 96 | try |
87 | 97 | { |
88 | 98 | foreach (var watcher in _watchers) |
89 | | - watcher.EnableRaisingEvents = true; |
| 99 | + watcher.EnableRaisingEvents = !ViewModels.Preferences.Instance.ManualRefresh; |
90 | 100 | } |
91 | 101 | catch |
92 | 102 | { |
93 | | - // Ignore exceptions. This may occur while `Dispose` is called. |
| 103 | + // Ignore failures while starting filesystem watchers. |
94 | 104 | } |
95 | | - }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void OnPreferencesChanged(object sender, PropertyChangedEventArgs e) |
| 109 | + { |
| 110 | + if (e.PropertyName != nameof(ViewModels.Preferences.ManualRefresh)) |
| 111 | + return; |
| 112 | + |
| 113 | + UpdateWatching(); |
| 114 | + if (!ViewModels.Preferences.Instance.ManualRefresh) |
| 115 | + OnWatcherError(null, null); // Reload changes missed while filesystem watchers were disabled. |
96 | 116 | } |
97 | 117 |
|
98 | 118 | public IDisposable Lock() |
@@ -128,20 +148,25 @@ public void MarkSubmodulesUpdated() |
128 | 148 |
|
129 | 149 | public void Dispose() |
130 | 150 | { |
131 | | - foreach (var watcher in _watchers) |
| 151 | + ViewModels.Preferences.Instance.PropertyChanged -= OnPreferencesChanged; |
| 152 | + lock (_syncRoot) |
132 | 153 | { |
133 | | - watcher.EnableRaisingEvents = false; |
134 | | - watcher.Dispose(); |
135 | | - } |
| 154 | + _disposed = true; |
| 155 | + foreach (var watcher in _watchers) |
| 156 | + { |
| 157 | + watcher.EnableRaisingEvents = false; |
| 158 | + watcher.Dispose(); |
| 159 | + } |
136 | 160 |
|
137 | | - _watchers.Clear(); |
138 | | - _timer.Dispose(); |
139 | | - _timer = null; |
| 161 | + _watchers.Clear(); |
| 162 | + _timer.Dispose(); |
| 163 | + _timer = null; |
| 164 | + } |
140 | 165 | } |
141 | 166 |
|
142 | 167 | private void Tick(object sender) |
143 | 168 | { |
144 | | - if (Interlocked.Read(ref _lockCount) > 0) |
| 169 | + if (ViewModels.Preferences.Instance.ManualRefresh || Interlocked.Read(ref _lockCount) > 0) |
145 | 170 | return; |
146 | 171 |
|
147 | 172 | var now = DateTime.Now.ToFileTime(); |
@@ -254,6 +279,9 @@ private void OnRepositoryChanged(object o, FileSystemEventArgs e) |
254 | 279 | /// </summary> |
255 | 280 | private void OnWatcherError(object o, ErrorEventArgs e) |
256 | 281 | { |
| 282 | + if (ViewModels.Preferences.Instance.ManualRefresh) |
| 283 | + return; |
| 284 | + |
257 | 285 | var desired = RecordChange(); |
258 | 286 | Interlocked.Exchange(ref _updateBranch, desired); |
259 | 287 | Interlocked.Exchange(ref _updateTags, desired); |
@@ -288,6 +316,9 @@ private void OnWorkingCopyChanged(object o, FileSystemEventArgs e) |
288 | 316 |
|
289 | 317 | private void HandleGitDirFileChanged(string name) |
290 | 318 | { |
| 319 | + if (ViewModels.Preferences.Instance.ManualRefresh) |
| 320 | + return; |
| 321 | + |
291 | 322 | if (name.Contains("fsmonitor--daemon/", StringComparison.Ordinal) || |
292 | 323 | name.EndsWith(".lock", StringComparison.Ordinal) || |
293 | 324 | name.StartsWith("lfs/", StringComparison.Ordinal)) |
@@ -346,6 +377,9 @@ private void HandleGitDirFileChanged(string name) |
346 | 377 |
|
347 | 378 | private void HandleWorkingCopyFileChanged(string name, string fullpath) |
348 | 379 | { |
| 380 | + if (ViewModels.Preferences.Instance.ManualRefresh) |
| 381 | + return; |
| 382 | + |
349 | 383 | if (name.StartsWith(".vs/", StringComparison.Ordinal)) |
350 | 384 | return; |
351 | 385 |
|
@@ -386,6 +420,8 @@ private long RecordChange() |
386 | 420 | } |
387 | 421 |
|
388 | 422 | private readonly IRepository _repo; |
| 423 | + private readonly object _syncRoot = new object(); |
| 424 | + private bool _disposed; |
389 | 425 | private readonly string _root; |
390 | 426 | private List<FileSystemWatcher> _watchers; |
391 | 427 | private Timer _timer; |
|
0 commit comments