11Imports System.Collections.ObjectModel
22Imports System.Collections.Specialized
3+ Imports System.Runtime
34Imports System.Text.Json
45Imports System.Threading
56
@@ -63,7 +64,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
6364 AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6465
6566
66- BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings )
67+ BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger)
6768
6869
6970 AddHandler BGCompactor.IsCompactingEvent, Sub (sender, isCompacting)
@@ -83,24 +84,46 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
8384 WatcherLog.SystemIdleDetected(_logger)
8485 BGCompactor.ResumeCompacting()
8586
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" )
8698 RemoveHandler IdleDetector.IsIdle, _idleHandler
8799
100+ For Each watcher In WatchedFolders
101+ watcher.PauseMonitoring()
102+ Next
103+
88104 Try
89- If Not IsWatchingEnabled Then Return
105+ If Not IsWatchingEnabled Then Return False
90106 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
92108
93109 If _parseWatchersSemaphore.CurrentCount <> 0 Then
94- Await ParseWatchers()
110+ Await ParseWatchers(runAll )
95111 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 )
98114 End If
115+ Return True
99116 Finally
100117
101118 AddHandler IdleDetector.IsIdle, _idleHandler
119+ For Each watcher In WatchedFolders
120+ watcher.ResumeMonitoring()
121+ Next
102122 End Try
103- End Sub
123+ Return False
124+ End Function
125+
126+
104127
105128 Private Sub OnSystemNotIdle(sender As Object , e As EventArgs)
106129 _isSystemIdle = False
@@ -366,7 +389,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
366389
367390 End Function
368391
369- Public Async Function BackgroundCompact() As Task
392+ Public Async Function BackgroundCompact( Optional runAll As Boolean = False ) As Task
370393
371394 Dim acquired = Await _parseWatchersSemaphore.WaitAsync( 0 )
372395 If Not acquired Then Return
@@ -375,11 +398,22 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
375398
376399 If BGCompactor.IsCompactorActive Then Return
377400
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)
381416
382- Await BGCompactor.StartCompactingAsync(WatchedFolders)
383417 OnPropertyChanged( NameOf (TotalSaved))
384418 Finally
385419 _parseWatchersSemaphore.Release()
0 commit comments