Skip to content

Commit 595541c

Browse files
committed
Simplified idle detector
1 parent 950034b commit 595541c

2 files changed

Lines changed: 44 additions & 39 deletions

File tree

CompactGUI.Watcher/IdleDetector.vb

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,63 @@
11
Imports System.Runtime.InteropServices
22
Imports System.Threading
33

4-
Public Class IdleDetector
5-
6-
Public Shared Event IsIdle As EventHandler
7-
Public Shared Event IsNotIdle As EventHandler
4+
Public Enum IdleState
5+
Idle
6+
NotIdle
7+
End Enum
88

9-
Private Shared _settings As IdleSettings
9+
Public Class IdleDetector
1010

11-
Private Shared _timerTask As Task
12-
Private Shared _idletimer As PeriodicTimer
13-
Private Shared ReadOnly _cts As New CancellationTokenSource
11+
Public Event IsIdle As EventHandler
12+
Public Event IsNotIdle As EventHandler
1413

15-
Public Shared Property Paused As Boolean = False
14+
Private ReadOnly _settings As IdleSettings
1615

17-
Public Shared Property IsEnabled As Boolean = True
16+
Private _timerTask As Task
17+
Private _idletimer As PeriodicTimer
18+
Private _cts As CancellationTokenSource
1819

19-
Public Shared Property IsAlreadyIdle As Boolean = False
20-
Public Shared Property LastIdleTime As DateTime = DateTime.MinValue
20+
Public Property State As IdleState
21+
Public Property LastIdleTime As DateTime = DateTime.MinValue
2122

2223

23-
Public Shared Sub Initialize(settings As IdleSettings)
24+
Public Sub New(settings As IdleSettings)
2425
_settings = settings
25-
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(_settings.IdleCheckIntervalSeconds))
26-
2726
End Sub
2827

2928

30-
Public Shared Sub Start()
31-
If _timerTask Is Nothing OrElse _timerTask.IsCompleted Then _timerTask = IdleTimerDoWorkAsync()
29+
Public Async Sub Start()
30+
Await StopAsync()
31+
_cts = New CancellationTokenSource()
32+
_idletimer = New PeriodicTimer(TimeSpan.FromSeconds(_settings.IdleCheckIntervalSeconds))
33+
_timerTask = IdleTimerDoWorkAsync()
34+
State = IdleState.NotIdle
3235
End Sub
3336

34-
Public Shared Async Sub StopAsync()
35-
_cts.Cancel()
37+
Public Async Function StopAsync() As Task
38+
_cts?.Cancel()
39+
_idletimer?.Dispose()
40+
_idletimer = Nothing
3641
If _timerTask IsNot Nothing Then Await _timerTask
37-
_idletimer.Dispose()
38-
_cts.Dispose()
39-
End Sub
40-
41-
Private Shared Async Function IdleTimerDoWorkAsync() As Task
42+
_cts?.Dispose()
43+
_cts = Nothing
44+
End Function
4245

46+
Private Async Function IdleTimerDoWorkAsync() As Task
4347
Try
4448
While Await _idletimer.WaitForNextTickAsync(_cts.Token) AndAlso Not _cts.Token.IsCancellationRequested
49+
Trace.WriteLine("Doing work!!!")
4550

46-
If GetIdleTime() > _settings.IdleThresholdSeconds AndAlso Not Paused AndAlso IsEnabled Then
47-
If Not IsAlreadyIdle OrElse DateTime.Now.AddSeconds(-_settings.IdleRepeatTimeSeconds) > LastIdleTime Then
48-
IsAlreadyIdle = True
51+
If GetIdleTime() > _settings.IdleThresholdSeconds Then
52+
If State <> IdleState.Idle OrElse DateTime.Now.AddSeconds(-_settings.IdleRepeatTimeSeconds) > LastIdleTime Then
53+
State = IdleState.Idle
4954
LastIdleTime = DateTime.Now
5055
RaiseEvent IsIdle(Nothing, EventArgs.Empty)
5156
End If
5257

53-
ElseIf Not Paused AndAlso IsEnabled Then
54-
If IsAlreadyIdle Then
55-
IsAlreadyIdle = False
58+
Else
59+
If State = IdleState.Idle Then
60+
State = IdleState.NotIdle
5661
RaiseEvent IsNotIdle(Nothing, EventArgs.Empty)
5762
End If
5863

CompactGUI.Watcher/Watcher.vb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
2727

2828
Private ReadOnly _logger As ILogger(Of Watcher)
2929
Private ReadOnly _settingsService As ISettingsService
30+
Private ReadOnly _idleDetector As IdleDetector
3031

3132
<NotifyPropertyChangedFor(NameOf(TotalSaved))>
3233
<ObservableProperty> Private _LastAnalysed As DateTime
@@ -52,15 +53,15 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
5253
WatcherJSONFile = New IO.FileInfo(IO.Path.Combine(_DataFolder.FullName, "watcher.json"))
5354

5455
IdleSettings = New IdleSettings
55-
IdleDetector.Initialize(IdleSettings)
56+
_idleDetector = New IdleDetector(IdleSettings)
5657

5758
WatcherLog.WatcherStarted(logger)
5859
IsActive = True
5960

6061

61-
IdleDetector.Start()
62-
AddHandler IdleDetector.IsIdle, _idleHandler
63-
AddHandler IdleDetector.IsNotIdle, AddressOf OnSystemNotIdle
62+
_idleDetector.Start()
63+
AddHandler _idleDetector.IsIdle, _idleHandler
64+
AddHandler _idleDetector.IsNotIdle, AddressOf OnSystemNotIdle
6465
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6566

6667

@@ -94,8 +95,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
9495
End Function
9596

9697
Public Async Function RunWatcher(Optional runAll As Boolean = True) As Task(Of Boolean)
97-
_logger.LogDebug("RunWatcher called")
98-
RemoveHandler IdleDetector.IsIdle, _idleHandler
98+
RemoveHandler _idleDetector.IsIdle, _idleHandler
9999

100100
For Each watcher In WatchedFolders
101101
watcher.PauseMonitoring()
@@ -115,7 +115,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
115115
Return True
116116
Finally
117117

118-
AddHandler IdleDetector.IsIdle, _idleHandler
118+
AddHandler _idleDetector.IsIdle, _idleHandler
119119
For Each watcher In WatchedFolders
120120
watcher.ResumeMonitoring()
121121
Next
@@ -142,7 +142,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
142142
_disableCounter += 1
143143
If _disableCounter = 1 Then
144144
WatcherLog.BackgroundingDisabled(_logger)
145-
IdleDetector.Paused = True
145+
Await _idleDetector.StopAsync()
146146
BGCompactor.CancelCompacting()
147147
Await _parseWatchersSemaphore.WaitAsync()
148148
End If
@@ -158,7 +158,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
158158
_disableCounter -= 1
159159
If _disableCounter = 0 Then
160160
_parseWatchersSemaphore.Release()
161-
IdleDetector.Paused = False
161+
_idleDetector.Start()
162162
WatcherLog.BackgroundingEnabled(_logger)
163163
End If
164164
End If

0 commit comments

Comments
 (0)