Skip to content

Commit 1171cec

Browse files
committed
Refactor idle detection and compacting logic
- Removed `isSystemIdle` field and related event handlers from `BackgroundCompactor`. - Simplified idle state checks in `BeginCompacting` method. - Added `LastIdleTime` property to `IdleDetector` for improved idle tracking. - Refactored idle event handling in `Watcher` to streamline compacting control. - Cleaned up code structure by removing redundant checks and flags.
1 parent d5e4eff commit 1171cec

4 files changed

Lines changed: 63 additions & 57 deletions

File tree

CompactGUI.Watcher/BackgroundCompactor.vb

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Public Class BackgroundCompactor
1515
Private isCompacting As Boolean = False
1616
Private isCompactingPaused As Boolean = False ' Track if compacting is paused
1717

18-
Private isSystemIdle As Boolean = False
19-
2018
Private _compactor As Core.Compactor
2119

2220
Private _excludedFileTypes As String()
@@ -31,28 +29,9 @@ Public Class BackgroundCompactor
3129
_excludedFileTypes = excludedFileTypes
3230
_logger = logger
3331
_idleSettings = settings
34-
AddHandler IdleDetector.IsIdle, AddressOf OnSystemIdle
35-
AddHandler IdleDetector.IsNotIdle, AddressOf OnSystemNotIdle
3632

3733
End Sub
3834

39-
Private Sub OnSystemIdle(sender As Object, e As EventArgs)
40-
If Not isSystemIdle Then WatcherLog.SystemIdleDetected(_logger)
41-
isSystemIdle = True
42-
' Attempt to resume only if compacting was paused due to system activity
43-
If isCompactingPaused AndAlso Not isCompacting Then
44-
ResumeCompacting()
45-
End If
46-
End Sub
47-
48-
Private Sub OnSystemNotIdle(sender As Object, e As EventArgs)
49-
If isSystemIdle Then WatcherLog.SystemNotIdle(_logger)
50-
isSystemIdle = False
51-
' Attempt to pause only if compacting is currently active and not already paused
52-
If isCompacting AndAlso Not isCompactingPaused Then
53-
PauseCompacting()
54-
End If
55-
End Sub
5635

5736
Public Function BeginCompacting(folder As String, compressionLevel As Core.WOFCompressionAlgorithm) As Task(Of Boolean)
5837

@@ -91,12 +70,12 @@ Public Class BackgroundCompactor
9170
While Not cancellationToken.IsCancellationRequested AndAlso Not compactingTask.IsCompleted
9271
Await Task.WhenAny(compactingTask, Task.Delay(1000, cancellationToken))
9372

94-
' Check the idle state and adjust compacting status accordingly
95-
If Not isSystemIdle AndAlso Not isCompactingPaused Then
96-
PauseCompacting()
97-
ElseIf isSystemIdle AndAlso isCompactingPaused Then
98-
ResumeCompacting()
99-
End If
73+
'' Check the idle state and adjust compacting status accordingly
74+
'If Not isSystemIdle AndAlso Not isCompactingPaused Then
75+
' PauseCompacting()
76+
'ElseIf isSystemIdle AndAlso isCompactingPaused Then
77+
' ResumeCompacting()
78+
'End If
10079
End While
10180

10281
Dim result = Await compactingTask
@@ -133,15 +112,23 @@ Public Class BackgroundCompactor
133112
End Function
134113

135114
Public Sub PauseCompacting()
115+
If Not isCompacting OrElse isCompactingPaused Then
116+
Return
117+
End If
118+
136119
WatcherLog.PausingBackgroundCompactor(_logger)
137120
isCompactingPaused = True ' Indicate compacting is paused
138-
_compactor.Pause()
121+
_compactor?.Pause()
139122
End Sub
140123

141124
Public Sub ResumeCompacting()
125+
If Not isCompactingPaused OrElse Not isCompacting Then
126+
Return
127+
End If
128+
142129
WatcherLog.ResumingBackgroundCompactor(_logger)
143130
isCompactingPaused = False ' Indicate compacting is no longer paused
144-
_compactor.Resume()
131+
_compactor?.Resume()
145132
End Sub
146133

147134
End Class

CompactGUI.Watcher/IdleDetector.vb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Public Class IdleDetector
1717
Public Shared Property IsEnabled As Boolean = True
1818

1919
Public Shared Property IsAlreadyIdle As Boolean = False
20+
Public Shared Property LastIdleTime As DateTime = DateTime.MinValue
2021

2122

2223
Public Shared Sub Initialize(settings As IdleSettings)
@@ -43,14 +44,18 @@ Public Class IdleDetector
4344
While Await _idletimer.WaitForNextTickAsync(_cts.Token) AndAlso Not _cts.Token.IsCancellationRequested
4445

4546
If GetIdleTime() > _settings.IdleThresholdSeconds AndAlso Not Paused AndAlso IsEnabled Then
46-
If Not IsAlreadyIdle Then
47+
If Not IsAlreadyIdle OrElse DateTime.Now.AddSeconds(-_settings.IdleRepeatTimeSeconds) > LastIdleTime Then
4748
IsAlreadyIdle = True
49+
LastIdleTime = DateTime.Now
4850
RaiseEvent IsIdle(Nothing, EventArgs.Empty)
4951
End If
5052

5153
ElseIf Not Paused AndAlso IsEnabled Then
52-
IsAlreadyIdle = False
53-
RaiseEvent IsNotIdle(Nothing, EventArgs.Empty)
54+
If IsAlreadyIdle Then
55+
IsAlreadyIdle = False
56+
RaiseEvent IsNotIdle(Nothing, EventArgs.Empty)
57+
End If
58+
5459
End If
5560

5661
End While

CompactGUI.Watcher/IdleSettings.vb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Public Class IdleSettings
2-
Public Property IdleCheckIntervalSeconds As Integer = 5
3-
Public Property IdleThresholdSeconds As Integer = 15
4-
Public Property LastSystemModifiedTimeThresholdSeconds As Integer = 300
2+
Public Property IdleCheckIntervalSeconds As Integer = 5 ' How often to check for idle state
3+
Public Property IdleThresholdSeconds As Integer = 120 ' Minimum seconds of inactivity to be considered idle
4+
Public Property IdleRepeatTimeSeconds As Integer = 60 ' How often to repeat firing the idle event after the initial idle state is detected
5+
Public Property LastSystemModifiedTimeThresholdSeconds As Integer = 300 ' How long to wait after the last system modification before considering a folder for analysis / compaction
56
End Class

CompactGUI.Watcher/Watcher.vb

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
5757

5858

5959
IdleDetector.Start()
60-
AddHandler IdleDetector.IsIdle, AddressOf OnSystemIdle
60+
AddHandler IdleDetector.IsIdle, _idleHandler
61+
AddHandler IdleDetector.IsNotIdle, AddressOf OnSystemNotIdle
6162
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6263

6364
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings)
@@ -66,6 +67,39 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
6667

6768
End Sub
6869

70+
Private _idleHandler As EventHandler = AddressOf OnSystemIdle
71+
Private _isSystemIdle As Boolean = False
72+
73+
Private Async Sub OnSystemIdle()
74+
_isSystemIdle = True
75+
WatcherLog.SystemIdleDetected(_logger)
76+
BGCompactor.ResumeCompacting()
77+
78+
RemoveHandler IdleDetector.IsIdle, _idleHandler
79+
80+
Try
81+
If Not IsWatchingEnabled Then Return
82+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
83+
If WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
84+
85+
If _parseWatchersSemaphore.CurrentCount <> 0 Then
86+
Await ParseWatchers()
87+
End If
88+
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
89+
Await BackgroundCompact()
90+
End If
91+
Finally
92+
93+
AddHandler IdleDetector.IsIdle, _idleHandler
94+
End Try
95+
End Sub
96+
97+
Private Sub OnSystemNotIdle(sender As Object, e As EventArgs)
98+
_isSystemIdle = False
99+
WatcherLog.SystemNotIdle(_logger)
100+
101+
BGCompactor.PauseCompacting()
102+
End Sub
69103

70104

71105
Private _disableCounter As Integer = 0
@@ -268,28 +302,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
268302
End Using
269303
End Function
270304

271-
Private _isHandlingIdle As Boolean = False
272-
273-
Private Async Function OnSystemIdle() As Task
274-
If _isHandlingIdle Then Return
275-
_isHandlingIdle = True
276-
Try
277-
278-
If Not IsWatchingEnabled Then Return
279-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
280-
If WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
281-
282-
If _parseWatchersSemaphore.CurrentCount <> 0 Then
283-
Await ParseWatchers()
284-
End If
285-
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
286-
Await BackgroundCompact()
287-
End If
288-
Finally
289305

290-
_isHandlingIdle = False
291-
End Try
292-
End Function
293306

294307

295308
Public Async Function ParseWatchers(Optional ParseAll As Boolean = False) As Task

0 commit comments

Comments
 (0)