Skip to content

Commit 5d010c2

Browse files
committed
Implemented Watcher cancellation
1 parent 1171cec commit 5d010c2

3 files changed

Lines changed: 67 additions & 13 deletions

File tree

CompactGUI.Watcher/BackgroundCompactor.vb

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ Imports Microsoft.Extensions.Logging.Abstractions
99

1010
Public Class BackgroundCompactor
1111

12-
Public Property IsCompactorActive As Boolean = False
12+
Private _IsCompactorActive As Boolean = False
13+
Public Property IsCompactorActive As Boolean
14+
Get
15+
Return _IsCompactorActive
16+
End Get
17+
Set(value As Boolean)
18+
If _IsCompactorActive = value Then Return
19+
_IsCompactorActive = value
20+
RaiseEvent IsCompactingEvent(Me, value)
21+
End Set
22+
End Property
1323

1424
Private cancellationTokenSource As New CancellationTokenSource()
1525
Private isCompacting As Boolean = False
@@ -24,6 +34,8 @@ Public Class BackgroundCompactor
2434

2535
Private ReadOnly _idleSettings As IdleSettings
2636

37+
Public Event IsCompactingEvent As EventHandler(Of Boolean)
38+
2739
Public Sub New(excludedFileTypes As String(), logger As ILogger(Of Watcher), settings As IdleSettings)
2840

2941
_excludedFileTypes = excludedFileTypes
@@ -45,7 +57,7 @@ Public Class BackgroundCompactor
4557

4658
Public Async Function StartCompactingAsync(folders As ObservableCollection(Of WatchedFolder)) As Task(Of Boolean)
4759
WatcherLog.BackgroundCompactingStarted(_logger)
48-
Dim cancellationToken As CancellationToken = cancellationTokenSource.Token
60+
cancellationTokenSource = New CancellationTokenSource()
4961

5062
IsCompactorActive = True
5163

@@ -67,16 +79,25 @@ Public Class BackgroundCompactor
6779
Dim compactingTask = BeginCompacting(folder.Folder, folder.CompressionLevel)
6880
isCompacting = True
6981

70-
While Not cancellationToken.IsCancellationRequested AndAlso Not compactingTask.IsCompleted
71-
Await Task.WhenAny(compactingTask, Task.Delay(1000, cancellationToken))
72-
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
79-
End While
82+
'While Not cancellationToken.IsCancellationRequested AndAlso Not compactingTask.IsCompleted AndAlso Not compactingTask.IsCanceled
83+
' Dim ret = Await compactingTask
84+
85+
'' Check the idle state and adjust compacting status accordingly
86+
'If Not isSystemIdle AndAlso Not isCompactingPaused Then
87+
' PauseCompacting()
88+
'ElseIf isSystemIdle AndAlso isCompactingPaused Then
89+
' ResumeCompacting()
90+
'End If
91+
'End While
92+
93+
If cancellationTokenSource.IsCancellationRequested Then
94+
Trace.WriteLine("Compacting cancelled by user.")
95+
folder.IsWorking = False
96+
IsCompactorActive = False
97+
isCompacting = False ' Ensure compacting status is reset after operation
98+
_compactor.Dispose()
99+
Return False
100+
End If
80101

81102
Dim result = Await compactingTask
82103
If result AndAlso folders.Contains(folder) Then
@@ -131,4 +152,17 @@ Public Class BackgroundCompactor
131152
_compactor?.Resume()
132153
End Sub
133154

155+
Public Sub CancelCompacting()
156+
If Not isCompacting Then
157+
Return
158+
End If
159+
Debug.WriteLine("Cancelling background compactor...")
160+
cancellationTokenSource.Cancel()
161+
cancellationTokenSource.Dispose()
162+
_compactor?.Cancel()
163+
_compactor?.Dispose()
164+
isCompacting = False
165+
isCompactingPaused = False ' Reset pause state on cancellation
166+
End Sub
167+
134168
End Class

CompactGUI.Watcher/Watcher.vb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Imports System.Text.Json
44
Imports System.Threading
55

66
Imports CommunityToolkit.Mvvm.ComponentModel
7+
Imports CommunityToolkit.Mvvm.Input
78
Imports CommunityToolkit.Mvvm.Messaging
89
Imports CommunityToolkit.Mvvm.Messaging.Messages
910

@@ -61,7 +62,14 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
6162
AddHandler IdleDetector.IsNotIdle, AddressOf OnSystemNotIdle
6263
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6364

65+
6466
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings)
67+
68+
69+
AddHandler BGCompactor.IsCompactingEvent, Sub(sender, isCompacting)
70+
CancelBackgroundingCommand.NotifyCanExecuteChanged()
71+
End Sub
72+
6573
InitializeWatchedFoldersAsync()
6674

6775

@@ -112,6 +120,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
112120
If _disableCounter = 1 Then
113121
WatcherLog.BackgroundingDisabled(_logger)
114122
IdleDetector.Paused = True
123+
BGCompactor.CancelCompacting()
115124
Await _parseWatchersSemaphore.WaitAsync()
116125
End If
117126
Finally
@@ -222,7 +231,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
222231
.LastCheckedDate = DateTime.Now
223232
.LastCheckedSize = newItem.LastCheckedSize
224233
.LastSystemModifiedDate = DateTime.Now
225-
.CompressionLevel = If(newItem.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION,newItem.CompressionLevel, existingItem.CompressionLevel)
234+
.CompressionLevel = If(newItem.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION, newItem.CompressionLevel, existingItem.CompressionLevel)
226235
End With
227236
existingItem.HasTargetChanged = False
228237
End Sub
@@ -427,6 +436,15 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
427436

428437
End Sub
429438

439+
<RelayCommand>
440+
Public Sub CancelBackgrounding()
441+
BGCompactor.CancelCompacting()
442+
CancelBackgroundingCommand.NotifyCanExecuteChanged()
443+
End Sub
444+
445+
Public Function CanCancelBackgrounding() As Boolean
446+
Return BGCompactor.IsCompactorActive
447+
End Function
430448

431449
End Class
432450

CompactGUI/Views/Components/FolderWatcherCard.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
Margin="-15,0,0,0"
8484
Foreground="#FFBFC7CE" IsIndeterminate="True"
8585
Visibility="{Binding RefreshWatchedCommand.IsRunning, Converter={StaticResource BoolToVisConverter}}" />
86+
87+
<Button Content="Cancel" Margin="-100" Command="{Binding Watcher.CancelBackgroundingCommand}"/>
8688
</Grid>
8789

8890

0 commit comments

Comments
 (0)