diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2024-04-07 12:12:28 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2024-04-07 12:12:28 +0300 |
| commit | 22680f5eb3acc11897363efba2730c7dc731d8ed (patch) | |
| tree | 6adffd3e809d8b7c38d1498406fa0bec890c5461 /Software/Visual_Studio | |
| parent | 83f7439d8230e593db77247da7a5cd6caf513343 (diff) | |
| download | Tango-22680f5eb3acc11897363efba2730c7dc731d8ed.tar.gz Tango-22680f5eb3acc11897363efba2730c7dc731d8ed.zip | |
Settings Mutex Per Twine Application.
Diffstat (limited to 'Software/Visual_Studio')
| -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(); + } + } } } } |
