Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static async Task<T> ReadJsonExSafe<T>(String fileName)
- {
- if (String.IsNullOrEmpty(fileName)) return default(T);
- string mutexName = "dependantOnApp" + fileName;
- return await TaskEx.Run(() =>
- {
- using (var myMutex = new Mutex(false, mutexName))
- {
- try
- {
- myMutex.WaitOne();
- using (var store = IsolatedStorageFile.GetUserStoreForApplication())
- using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
- using (var sr = new StreamReader(stream))
- using (var jr = new JsonTextReader(sr))
- return (T)jsonSerializer.Deserialize(jr);
- }
- catch { throw new Exception("Exception"); }
- finally { myMutex.ReleaseMutex(); }
- }
- });
- }
- public static async Task<bool> WriteJsonExSafe<T>(String fileName, T content)
- {
- if (String.IsNullOrEmpty(fileName))
- return false;
- string mutexName = "dependantOnApp" + fileName;
- return await TaskEx.Run(() =>
- {
- using (var myMutex = new Mutex(false, mutexName))
- {
- try
- {
- myMutex.WaitOne();
- using (var store = IsolatedStorageFile.GetUserStoreForApplication())
- {
- store.CreateFolderIfNecessary(fileName);
- using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, FileAccess.Write, store))
- using (var sw = new StreamWriter(stream))
- using (var jw = new JsonTextWriter(sw))
- jsonSerializer.Serialize(jw, content);
- }
- }
- catch { throw new Exception("Exception"); }
- finally { myMutex.ReleaseMutex(); }
- return true;
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment