浏览代码

Attempt to save multiple times

Something as simple as an antivirus in the middle of a scan could make a save fail.

Should, along with all the other changes, fix #7.
Daniel Chalmers 1 年之前
父节点
当前提交
a91f543df7
共有 1 个文件被更改,包括 19 次插入16 次删除
  1. 19 16
      DesktopClock/Properties/Settings.cs

+ 19 - 16
DesktopClock/Properties/Settings.cs

@@ -87,10 +87,26 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
     /// <summary>
     /// Saves to the default path in JSON format.
     /// </summary>
-    public void Save()
+    public bool Save()
     {
         var json = JsonConvert.SerializeObject(this, _jsonSerializerSettings);
-        File.WriteAllText(FilePath, json);
+
+        // Attempt to save up to 4 times.
+        for (var i = 0; i < 4; i++)
+        {
+            try
+            {
+                File.WriteAllText(FilePath, json);
+                return true;
+            }
+            catch
+            {
+                // Wait before next attempt to read.
+                System.Threading.Thread.Sleep(250);
+            }
+        }
+
+        return false;
     }
 
     /// <summary>
@@ -123,20 +139,7 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
     {
         var settings = LoadFromFile();
 
-        try
-        {
-            settings.Save();
-
-            CanBeSaved = true;
-        }
-        catch (UnauthorizedAccessException)
-        {
-            CanBeSaved = false;
-        }
-        catch (IOException)
-        {
-            CanBeSaved = false;
-        }
+        CanBeSaved = settings.Save();
 
         return settings;
     }