@@ -9,7 +9,17 @@ Imports Microsoft.Extensions.Logging.Abstractions
99
1010Public 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+
134168End Class
0 commit comments