I tried to implement a write read operation using File IO operations and encapsulated these operations in a TransformBlock to make these operations threads safe instead of using a locking mechanism.
But the problem is that when I try to write even 5 files in parallel, there is memory from the exception and when using this implementation it blocks the user interface stream. Implementation is carried out in the Windows Phone project. Please imagine what is wrong with this incarnation.
File I / O operation
public static readonly IsolatedStorageFile _isolatedStore = IsolatedStorageFile.GetUserStoreForApplication(); public static readonly FileIO _file = new FileIO(); public static readonly ConcurrentExclusiveSchedulerPair taskSchedulerPair = new ConcurrentExclusiveSchedulerPair(); public static readonly ExecutionDataflowBlockOptions exclusiveExecutionDataFlow = new ExecutionDataflowBlockOptions { TaskScheduler = taskSchedulerPair.ExclusiveScheduler, BoundedCapacity = 1 }; public static readonly ExecutionDataflowBlockOptions concurrentExecutionDataFlow = new ExecutionDataflowBlockOptions { TaskScheduler = taskSchedulerPair.ConcurrentScheduler, BoundedCapacity = 1 }; public static async Task<T> LoadAsync<T>(string fileName) { T result = default(T); var transBlock = new TransformBlock<string, T> (async fName => { return await LoadData<T>(fName); }, concurrentExecutionDataFlow); transBlock.Post(fileName); result = await transBlock.ReceiveAsync(); return result; } public static async Task SaveAsync<T>(T obj, string fileName) { var transBlock = new TransformBlock<Tuple<T, string>, Task> (async tupleData => { await SaveData(tupleData.Item1, tupleData.Item2); }, exclusiveExecutionDataFlow); transBlock.Post(new Tuple<T, string>(obj, fileName)); await transBlock.ReceiveAsync(); }
MainPage.xaml.cs Usage
private static string data = "vjdsskjfhkjsdhvnvndjfhjvkhdfjkgd" private static string fileName = string.Empty; private List<string> DataLstSample = new List<string>(); private ObservableCollection<string> TestResults = new ObservableCollection<string>(); private static string data1 = "hjhkjhkhkjhjkhkhkjhkjhkhjkhjkh"; List<Task> allTsk = new List<Task>(); private Random rand = new Random(); private string fileNameRand { get { return rand.Next(100).ToString(); } } public MainPage() { InitializeComponent(); for (int i = 0; i < 5; i ++) { DataLstSample.Add((i % 2) == 0 ? data : data1); } } private void Button_Click(object sender, RoutedEventArgs e) { AppIsolatedStore_TestInMultiThread_LstResultShouldBeEqual(); } public async void AppIsolatedStore_TestInMultiThread_LstResultShouldBeEqual() { TstRst.Text = "InProgress.."; allTsk.Clear(); foreach(var data in DataLstSample) { var fName = fileNameRand; var t = Task.Run(async () => { await AppIsolatedStore.SaveAsync<string>(data, fName); }); TestResults.Add(string.Format("Writing file name: {0}, data: {1}", fName, data)); allTsk.Add(t); } await Task.WhenAll(allTsk); TstRst.Text = "Completed.."; }
Save and load Async data
Edit:
The reason it has a memory exception is due to one reason that the data row I took is too long. The string is a link: http://1drv.ms/1QWSAsc
But the second problem is that if I add small data, then it blocks the UI thread. Is the code any task to run the UI?
source share