1
1
Imports System.Collections.ObjectModel
2
2
Imports System.Collections.Specialized
3
+ Imports System.Runtime
3
4
Imports System.Text.Json
4
5
Imports System.Threading
5
6
@@ -63,7 +64,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
63
64
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
64
65
65
66
66
- BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings )
67
+ BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger)
67
68
68
69
69
70
AddHandler BGCompactor.IsCompactingEvent, Sub (sender, isCompacting)
@@ -83,24 +84,46 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
83
84
WatcherLog.SystemIdleDetected(_logger)
84
85
BGCompactor.ResumeCompacting()
85
86
87
+ Await RunWatcher( False )
88
+
89
+ End Sub
90
+
91
+ <RelayCommand>
92
+ Public Async Function RunWatcher() As Task( Of Boolean )
93
+ Return Await RunWatcher( True )
94
+ End Function
95
+
96
+ Public Async Function RunWatcher( Optional runAll As Boolean = True ) As Task( Of Boolean )
97
+ _logger.LogDebug( "RunWatcher called" )
86
98
RemoveHandler IdleDetector.IsIdle, _idleHandler
87
99
100
+ For Each watcher In WatchedFolders
101
+ watcher.PauseMonitoring()
102
+ Next
103
+
88
104
Try
89
- If Not IsWatchingEnabled Then Return
105
+ If Not IsWatchingEnabled Then Return False
90
106
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
91
- If WatchedFolders.Any( Function (x) x.LastChangedDate > recentThresholdDate) Then Return
107
+ If Not runAll AndAlso WatchedFolders.Any( Function (x) x.LastChangedDate > recentThresholdDate) Then Return False
92
108
93
109
If _parseWatchersSemaphore.CurrentCount <> 0 Then
94
- Await ParseWatchers()
110
+ Await ParseWatchers(runAll )
95
111
End If
96
- If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
97
- Await BackgroundCompact()
112
+ If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso ( IsBackgroundCompactingEnabled OrElse runAll) Then
113
+ Await BackgroundCompact(runAll )
98
114
End If
115
+ Return True
99
116
Finally
100
117
101
118
AddHandler IdleDetector.IsIdle, _idleHandler
119
+ For Each watcher In WatchedFolders
120
+ watcher.ResumeMonitoring()
121
+ Next
102
122
End Try
103
- End Sub
123
+ Return False
124
+ End Function
125
+
126
+
104
127
105
128
Private Sub OnSystemNotIdle(sender As Object , e As EventArgs)
106
129
_isSystemIdle = False
@@ -366,7 +389,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
366
389
367
390
End Function
368
391
369
- Public Async Function BackgroundCompact() As Task
392
+ Public Async Function BackgroundCompact( Optional runAll As Boolean = False ) As Task
370
393
371
394
Dim acquired = Await _parseWatchersSemaphore.WaitAsync( 0 )
372
395
If Not acquired Then Return
@@ -375,11 +398,22 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
375
398
376
399
If BGCompactor.IsCompactorActive Then Return
377
400
378
- If Not WatchedFolders.Any( Function (f) f.DecayPercentage <> 0 AndAlso f.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION) Then
379
- Return
380
- End If
401
+ Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
402
+
403
+ Dim foldersToCompress = WatchedFolders.
404
+ Where ( Function (folder)
405
+ Dim eligible = folder.DecayPercentage <> 0 AndAlso folder.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION
406
+ Dim recentlyModified = folder.LastSystemModifiedDate > recentThresholdDate AndAlso Not runAll
407
+ If eligible AndAlso recentlyModified Then
408
+ WatcherLog.SkippingRecentlyModifiedFolder(_logger, folder.DisplayName)
409
+ End If
410
+ Return eligible AndAlso Not recentlyModified
411
+ End Function )
412
+
413
+ If foldersToCompress.Any = 0 Then Return
414
+
415
+ Await BGCompactor.StartCompactingAsync(foldersToCompress)
381
416
382
- Await BGCompactor.StartCompactingAsync(WatchedFolders)
383
417
OnPropertyChanged( NameOf (TotalSaved))
384
418
Finally
385
419
_parseWatchersSemaphore.Release()
0 commit comments