private static async Task LoadQueueInternalAsync(IotHubClient client) { System.Collections.Generic.Queue<DataPoint> tmp = new System.Collections.Generic.Queue<DataPoint>(); try { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile file = await storageFolder.GetFileAsync(FILE_NAME); var stream = await file.OpenAsync(FileAccessMode.ReadWrite); ulong size = stream.Size; using (var inputStream = stream.GetInputStreamAt(0)) { using (var dataReader = new DataReader(inputStream)) { uint numBytesLoaded = await dataReader.LoadAsync((uint)size); int count = dataReader.ReadInt32(); for (int i = 0; i < count; i++) { DataPoint p = new DataPoint(); p.Read(dataReader); tmp.Enqueue(p); } } } stream.Dispose(); lock (client.thisLock) { client.queue.Clear(); client.queue = null; client.queue = new System.Collections.Generic.Queue<DataPoint>(tmp); } } catch (Exception) { } }
private static async Task SaveQueueInternalAsync(IotHubClient client) { try { System.Collections.Generic.Queue<DataPoint> tmp = null; lock (client.thisLock) { tmp = new System.Collections.Generic.Queue<DataPoint>(client.queue); } StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile file = await storageFolder.CreateFileAsync(FILE_NAME, CreationCollisionOption.ReplaceExisting); var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); using (var outputStream = stream.GetOutputStreamAt(0)) { using (var dataWriter = new DataWriter(outputStream)) { dataWriter.WriteInt32(tmp.Count); foreach (var item in tmp) { item.Write(dataWriter); } await dataWriter.StoreAsync(); await outputStream.FlushAsync(); } } stream.Dispose(); } catch (Exception) { } }