Vitalii

Untitled

May 11th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. private static readonly AwaitableCriticalSection _criticalSection = new AwaitableCriticalSection();
  2.  
  3.         public static async Task<T> ReadJsonExCs<T>(String fileName)
  4.         {
  5.             if (String.IsNullOrEmpty(fileName))
  6.                 return default(T);
  7.  
  8.             return await await Task.Factory.StartNew(async () =>
  9.             {
  10.                 using (var section = await _criticalSection.EnterAsync())
  11.                 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
  12.                 using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
  13.                 using (var sr = new StreamReader(stream))
  14.                 using (var jr = new JsonTextReader(sr))
  15.                     return await jr.ReadJsonAsyncTask<T>();
  16.             });
  17.         }
  18.        
  19.         public static async Task<bool> WriteJsonExCs<T>(String fileName, T content)
  20.         {
  21.             if (String.IsNullOrEmpty(fileName))
  22.                 return false;
  23.  
  24.             return await await Task.Factory.StartNew(async () =>
  25.             {
  26.                 using (var section = await _criticalSection.EnterAsync())
  27.                 using (var store = IsolatedStorageFile.GetUserStoreForApplication())
  28.                 {
  29.                     store.CreateFolderIfNecessary(fileName);
  30.  
  31.                     using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
  32.                     using (var sw = new StreamWriter(stream))
  33.                     using (var jw = new JsonTextWriter(sw))
  34.                         await jw.WriteJsonAsyncTask(content);
  35.                 }
  36.  
  37.                 return true;
  38.             });
  39.         }
Advertisement
Add Comment
Please, Sign In to add comment