/// <summary> /// 反序列化文件 /// </summary> /// <param name="file"></param> /// <returns></returns> private async Task DeserializeFile(FileInfo file) { CT.Debug($"读取 {file.FullName}", "File"); if (!File.Exists(file.FullName)) { UpdateMemoryValuesFromExternalValues(KeyValues, OriginalKeyValues, new Dictionary <string, string>()); OriginalKeyValues.Clear(); return; } const int retryCount = 100; for (var i = 0; i < retryCount; i++) { try { // 一次性读取完的性能最好 var str = File.ReadAllText(file.FullName); Deserialize(str); _lastDeserializeTime = DateTimeOffset.Now; return; } catch (IOException) { const int waitTime = 10; // 读取配置文件出现异常,忽略所有异常 await Task.Delay(waitTime).ConfigureAwait(false); // 通过测试发现在我的设备,写入平均时间是 6 毫秒,也就是如果存在多实例写入,也不会是 waitTime*retryCount 毫秒这么久,等待 waitTime*retryCount 毫秒也是可以接受最大的值 } #pragma warning disable CA1031 // Do not catch general exception types catch #pragma warning restore CA1031 // Do not catch general exception types { // 这里的代码因为从一个 IO 线程调进来,所以当调用方使用 await 等待,会使得这里抛出的异常回到 IO 线程,导致应用程序崩溃。 // 这里可能的异常有: // - UnauthorizedAccessException 在文件只读、文件实际上是一个文件夹、没有读权限或者平台不支持时引发。 const int waitTime = 10; await Task.Delay(waitTime).ConfigureAwait(false); } } }
public bool UpdateDifferences() { // Obtain actual status Dictionary <string, object> new_key_values = GetCurrentKeyValues(); List <string> new_subs = GetCurrentKeySubkeys(); // Compare and save differences // Look for new subkeys: ones that are now present and were not present before. NewSubeys = new List <string>(); foreach (var newsub in new_subs) { if (!OriginalSubkeys.Contains(newsub)) { NewSubeys.Add(newsub); } } // Look for deleted subkeys: ones that are now inexistent and were present before. DeletedSubkeys = new List <string>(); foreach (var oldsub in OriginalSubkeys) { if (!new_subs.Contains(oldsub)) { DeletedSubkeys.Add(oldsub); } } // Look for deleted values: ones that are now inexistent and were present before. DeletedValues = new Dictionary <string, object>(); foreach (var old_key_value in OriginalKeyValues) { if (!new_key_values.ContainsKey(old_key_value.Key)) { DeletedValues.Add(old_key_value.Key, old_key_value.Value); } } // Look for new values: ones that didn't exist at the beginning but are now present NewValues = new Dictionary <string, object>(); foreach (var new_key_value in new_key_values) { if (!OriginalKeyValues.ContainsKey(new_key_value.Key)) { NewValues.Add(new_key_value.Key, new_key_value.Value); } } // Look for modified values: present before and now but with different values ModifiedValues = new Dictionary <string, object>(); foreach (var new_key_value in new_key_values) { if (OriginalKeyValues.ContainsKey(new_key_value.Key)) { // Add it to the list only if the value has changed or if the type has changed if (new_key_value.Value != null && OriginalKeyValues[new_key_value.Key] != null && new_key_value.Value.ToString() != OriginalKeyValues[new_key_value.Key].ToString()) { ModifiedValues.Add(new_key_value.Key, new { OriginalValue = OriginalKeyValues[new_key_value.Key], CurrentValue = new_key_value.Value, }); } } } return(NewSubeys.Count > 0 || DeletedSubkeys.Count > 0 || DeletedValues.Count > 0 || NewValues.Count > 0 || ModifiedValues.Count > 0); }