diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Settings')
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/SettingsManager.cs | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs index 3a30ffbbb..0383af5be 100644 --- a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs +++ b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs @@ -4,7 +4,9 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; using Tango.Logging; @@ -18,6 +20,7 @@ namespace Tango.Settings #region Singleton private static object _syncLock = new object(); + private static String MUTEX_NAME = Assembly.GetEntryAssembly().GetName().Name; private static SettingsManager _default; /// <summary> @@ -153,9 +156,38 @@ namespace Tango.Settings { EnsureLoaded(); - _logManager.Log("Saving settings to " + FilePath + "..."); - String json = JsonConvert.SerializeObject(_settingsCollection, _jsonSettings); - File.WriteAllText(FilePath, json); + using (var mutex = new Mutex(false, MUTEX_NAME)) + { + var mutexAcquired = false; + try + { + mutexAcquired = mutex.WaitOne(5000); + } + catch (AbandonedMutexException) + { + mutexAcquired = true; + } + + // if it wasn't acquired, it timed out, so can handle that how ever we want + if (!mutexAcquired) + { + _logManager.Log("Error saving settings due to Mutex not acquired"); + return; + } + + // otherwise, we've acquired the mutex and should do what we need to do, + // then ensure that we always release the mutex + try + { + _logManager.Log("Saving settings to " + FilePath + "..."); + String json = JsonConvert.SerializeObject(_settingsCollection, _jsonSettings); + File.WriteAllText(FilePath, json); + } + finally + { + mutex.ReleaseMutex(); + } + } } } } |
