@@ -50,11 +50,11 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
5050 _logger = logger
5151 _settingsService = settingsService
5252 _DataFolder = settingsService.DataFolder
53+
5354 WatcherJSONFile = New IO.FileInfo(IO.Path.Combine(_DataFolder.FullName, "watcher.json" ))
5455
5556 IdleSettings = New IdleSettings
5657 _idleDetector = idleDetector
57-
5858 WatcherLog.WatcherStarted(logger)
5959 IsActive = True
6060
@@ -66,10 +66,6 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
6666 BGCompactor = New BackgroundCompactor(Array.Empty( Of String ), _logger)
6767
6868
69- AddHandler BGCompactor.IsCompactingEvent, Sub (sender, isCompacting)
70- CancelBackgroundingCommand.NotifyCanExecuteChanged()
71- End Sub
72-
7369 InitializeWatchedFoldersAsync()
7470
7571
@@ -92,39 +88,50 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
9288
9389 End Sub
9490
95- <RelayCommand>
96- Public Async Function RunWatcher() As Task( Of Boolean )
97- Return Await RunWatcher( True )
98- End Function
9991
100- Public Async Function RunWatcher( Optional runAll As Boolean = True ) As Task( Of Boolean )
92+
93+ <ObservableProperty> Private _isRunning As Boolean = False
94+
95+ Public Async Function RunWatcher( Optional runAll As Boolean = True , Optional cToken As CancellationToken = Nothing ) As Task( Of Boolean )
10196 RemoveHandler _idleDetector.IsIdle, _idleHandler
10297
103- Trace.WriteLine( "Watcher: RunWatcher called" )
98+ IsRunning = True
99+
104100 For Each watcher In WatchedFolders
105101 watcher.PauseMonitoring()
106102 Next
107103
108104 Try
109- Dim now = DateTime.Now
110- If Not IsWatchingEnabled Then Return False
111- Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
112- If Not runAll AndAlso WatchedFolders.Any( Function (x) x.LastChangedDate > recentThresholdDate) Then Return False
105+ Await Task.Run( Async Function ()
106+ _settingsService.AppSettings.ScheduledBackgroundLastRan = DateTime.Now
107+ If Not IsWatchingEnabled Then Return False
108+ Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
109+ If Not runAll AndAlso WatchedFolders.Any( Function (x) x.LastChangedDate > recentThresholdDate) Then Return False
110+
111+ If _parseWatchersSemaphore.CurrentCount <> 0 Then
112+ Await ParseWatchers(runAll, cToken)
113+ End If
114+ If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then
115+ _logger.LogInformation( "Watcher run cancelled by user." )
116+ Return False
117+ End If
118+ If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso (IsBackgroundCompactingEnabled OrElse runAll) Then
119+ Await BackgroundCompact(runAll) 'Don't need to pass the cancellation token here, as the background compactor handles it internally.
120+ End If
121+ Return True
122+ End Function , cToken)
123+
113124
114- If _parseWatchersSemaphore.CurrentCount <> 0 Then
115- Await ParseWatchers(runAll)
116- End If
117- If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso (IsBackgroundCompactingEnabled OrElse runAll) Then
118- Await BackgroundCompact(runAll)
119- End If
120- _settingsService.AppSettings.ScheduledBackgroundLastRan = now
121125 Return True
126+ Catch ex As OperationCanceledException
127+ Return False
122128 Finally
123129
124130 AddHandler _idleDetector.IsIdle, _idleHandler
125131 For Each watcher In WatchedFolders
126132 watcher.ResumeMonitoring()
127133 Next
134+ IsRunning = False
128135 End Try
129136 Return False
130137 End Function
@@ -347,7 +354,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
347354
348355
349356
350- Public Async Function ParseWatchers( Optional ParseAll As Boolean = False ) As Task
357+ Public Async Function ParseWatchers( Optional ParseAll As Boolean = False , Optional cToken As CancellationToken = Nothing ) As Task
351358 Dim acquired = Await _parseWatchersSemaphore.WaitAsync( 0 )
352359 If Not acquired Then Return
353360
@@ -364,9 +371,11 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
364371
365372 For Each fsWatcher In WatchersQuery
366373 WatcherLog.FolderChanged(_logger, fsWatcher.DisplayName)
367- Await Analyse(fsWatcher.Folder, ParseAll)
374+ If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then Return
375+ Await Analyse(fsWatcher.Folder, ParseAll, cToken)
368376 Next
369377
378+ If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then Return
370379 Await WriteToFileAsync()
371380 LastAnalysed = DateTime.Now
372381 Finally
@@ -433,37 +442,44 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
433442 End Function
434443
435444
436- Public Async Function Analyse(folder As String , checkDiskModified As Boolean ) As Task( Of Boolean )
445+ Public Async Function Analyse(folder As String , checkDiskModified As Boolean , Optional cToken As CancellationToken = Nothing ) As Task( Of Boolean )
437446
438447 Using analyser As New Analyser(folder, NullLogger( Of Analyser).Instance)
439448 Dim watched = WatchedFolders.First( Function (f) f.Folder = folder)
440449 watched.IsWorking = True
450+ Try
451+ Dim analysedFiles = Await analyser.GetAnalysedFilesAsync(cToken)
452+ If cToken <> Nothing AndAlso cToken.IsCancellationRequested Then Return False
441453
442- Dim analysedFiles = Await analyser.GetAnalysedFilesAsync(CancellationToken.None)
443-
444- watched.LastCheckedDate = DateTime.Now
445- watched.LastCheckedSize = analyser.CompressedBytes
446- watched.LastUncompressedSize = analyser.UncompressedBytes
454+ watched.LastCheckedDate = DateTime.Now
455+ watched.LastCheckedSize = analyser.CompressedBytes
456+ watched.LastUncompressedSize = analyser.UncompressedBytes
447457
448- watched.LastSystemModifiedDate = watched.LastChangedDate
458+ watched.LastSystemModifiedDate = watched.LastChangedDate
449459
450- If analysedFiles.Count <> 0 Then
451- Dim mainCompressionLVL = analysedFiles?.Select( Function (f) f.CompressionMode).Max
452- watched.CompressionLevel = If (mainCompressionLVL <> WOFCompressionAlgorithm.NO_COMPRESSION, mainCompressionLVL, watched.CompressionLevel)
460+ If analysedFiles.Count <> 0 Then
461+ Dim mainCompressionLVL = analysedFiles?.Select( Function (f) f.CompressionMode).Max
462+ watched.CompressionLevel = If (mainCompressionLVL <> WOFCompressionAlgorithm.NO_COMPRESSION, mainCompressionLVL, watched.CompressionLevel)
453463
454- If checkDiskModified Then
455- Dim lastDiskWriteTime = analysedFiles.Select( Function (fl)
456- Dim finfo As New IO.FileInfo(fl.FileName)
457- Return finfo.LastWriteTime
458- End Function ).OrderByDescending( Function (f) f).First
464+ If checkDiskModified Then
465+ Dim lastDiskWriteTime = analysedFiles.Select( Function (fl)
466+ Dim finfo As New IO.FileInfo(fl.FileName)
467+ Return finfo.LastWriteTime
468+ End Function ).OrderByDescending( Function (f) f).First
459469
460- watched.LastSystemModifiedDate = If (watched.LastSystemModifiedDate < lastDiskWriteTime, lastDiskWriteTime, watched.LastSystemModifiedDate)
470+ watched.LastSystemModifiedDate = If (watched.LastSystemModifiedDate < lastDiskWriteTime, lastDiskWriteTime, watched.LastSystemModifiedDate)
461471
472+ End If
462473 End If
463- End If
464474
465- watched.HasTargetChanged = False
466- watched.IsWorking = False
475+ watched.HasTargetChanged = False
476+ Catch ex As OperationCanceledException
477+ Return False
478+ Finally
479+
480+ watched.IsWorking = False
481+ End Try
482+
467483 Return True
468484
469485 End Using
@@ -480,15 +496,9 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
480496
481497 End Sub
482498
483- <RelayCommand>
484- Public Sub CancelBackgrounding()
485- BGCompactor.CancelCompacting()
486- CancelBackgroundingCommand.NotifyCanExecuteChanged()
487- End Sub
488499
489- Public Function CanCancelBackgrounding() As Boolean
490- Return BGCompactor.IsCompactorActive
491- End Function
500+
501+
492502
493503End Class
494504
0 commit comments