/// <summary> /// Performs a threadsafe action on the storage content. Will load persisted data if not done. /// </summary> /// <param name="action">Action that is performed threadsafe on storage content.</param> /// <param name="save">After a successful action the storage content will be persisted.</param> public async Task Perform(Action <StorageContent> action, bool save = false) { await storageLock.WaitAsync(); if (storageContent == null) { await UnsafeOpen(); } try { action(storageContent); if (save) { await _storageIO.WriteAllTextAsync( _name, _converter.SerializeObject(storageContent)); } } catch (Exception) { throw; } finally { storageLock.Release(); } }
/// <summary> /// Persists current state. /// </summary> public async Task <bool> Save() { await storageLock.WaitAsync(); try { await _storageIO.WriteAllTextAsync( _name, _converter.SerializeObject(Content)); } catch (Exception) { return(false); } finally { storageLock.Release(); } return(true); }