浏览代码

Sort out threading issues in AlertsManager

Fixes #208
Antony Male 9 年之前
父节点
当前提交
bd2df48ae7
共有 1 个文件被更改,包括 19 次插入16 次删除
  1. 19 16
      src/SyncTrayzor/Services/AlertsManager.cs

+ 19 - 16
src/SyncTrayzor/Services/AlertsManager.cs

@@ -29,11 +29,17 @@ namespace SyncTrayzor.Services
         public bool AnyAlerts => this.ConflictedFiles.Count > 0 || this.FoldersWithFailedTransferFiles.Count > 0;
 
 
-        private readonly List<string> _conflictedFiles = new List<string>();
-        public IReadOnlyList<string> ConflictedFiles { get; private set; }
+        private IReadOnlyList<string> _conflictedFiles = EmptyReadOnlyList<string>.Instance;
+        public IReadOnlyList<string> ConflictedFiles
+        {
+            get { return this._enableConflictedFileAlerts ? this._conflictedFiles : EmptyReadOnlyList<string>.Instance; }
+        }
 
-        private readonly List<string> _foldersWithFailedTransferFiles = new List<string>();
-        public IReadOnlyList<string> FoldersWithFailedTransferFiles { get; private set; }
+        private IReadOnlyList<string> _foldersWithFailedTransferFiles = EmptyReadOnlyList<string>.Instance;
+        public IReadOnlyList<string> FoldersWithFailedTransferFiles
+        {
+            get { return this._enableFailedTransferAlerts ? this._foldersWithFailedTransferFiles : EmptyReadOnlyList<string>.Instance; }
+        }
 
         public event EventHandler AlertsStateChanged;
 
@@ -46,7 +52,7 @@ namespace SyncTrayzor.Services
                 if (this._enableFailedTransferAlerts == value)
                     return;
                 this._enableFailedTransferAlerts = value;
-                this.ResetOutputs();
+                this.OnAlertsStateChanged();
             }
         }
 
@@ -59,7 +65,7 @@ namespace SyncTrayzor.Services
                 if (this._enableConflictedFileAlerts == value)
                     return;
                 this._enableConflictedFileAlerts = value;
-                this.ResetOutputs();
+                this.OnAlertsStateChanged();
             }
         }
 
@@ -69,32 +75,29 @@ namespace SyncTrayzor.Services
             this.conflictFileWatcher = conflictFileWatcher;
             this.eventDispatcher = new SynchronizedEventDispatcher(this);
 
-            this.ResetOutputs();
-
             this.syncthingManager.Folders.FolderErrorsChanged += this.FolderErrorsChanged;
 
             this.conflictFileWatcher.ConflictedFilesChanged += this.ConflictFilesChanged;
         }
 
-        private void ResetOutputs()
+        private void OnAlertsStateChanged()
         {
-            this.FoldersWithFailedTransferFiles = this.EnableFailedTransferAlerts ? this._foldersWithFailedTransferFiles.AsReadOnly() : EmptyReadOnlyList<string>.Instance;
-            this.ConflictedFiles = this.EnableConflictedFileAlerts ? this._conflictedFiles.AsReadOnly() : EmptyReadOnlyList<string>.Instance;
-
             this.eventDispatcher.Raise(this.AlertsStateChanged);
         }
 
         private void FolderErrorsChanged(object sender, FolderErrorsChangedEventArgs e)
         {
             var folders = this.syncthingManager.Folders.FetchAll();
-            this._foldersWithFailedTransferFiles.Replace(folders.Where(x => x.FolderErrors.Any()).Select(x => x.FolderId));
-            this.eventDispatcher.Raise(this.AlertsStateChanged);
+            this._foldersWithFailedTransferFiles = folders.Where(x => x.FolderErrors.Any()).Select(x => x.FolderId).ToList().AsReadOnly();
+ 
+            this.OnAlertsStateChanged();
         }
 
         private void ConflictFilesChanged(object sender, EventArgs e)
         {
-            this._conflictedFiles.Replace(this.conflictFileWatcher.ConflictedFiles);
-            this.eventDispatcher.Raise(this.AlertsStateChanged);
+            this._conflictedFiles = this.conflictFileWatcher.ConflictedFiles.ToList().AsReadOnly();
+
+            this.OnAlertsStateChanged();
         }
 
         public void Dispose()