private void readDataFromMemory() { if (mapFile == null) { try { mapFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, fileName); } catch { mapFile = null; throw new Exception("File " + fileName + " is not opened yet."); } } try { reader = mapFile.MapView(MapAccess.FileMapRead, 0, 8 * 1024); } catch (Exception) { Debug.WriteLine("Error in Reading"); } getObjectFromMMF <TestData>(); }
private void openOrCreateMemoryAndWriteData <T>(T data) { mutex.WaitOne(); try { mapFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, fileName); } catch { mapFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, 8 * 1024, fileName); } Stream writer = mapFile.MapView(MapAccess.FileMapAllAccess, 0, 8 * 1024); char[] charArrayOfObject = serializeToXml <T>(data).ToCharArray(); for (int i = 0; i < charArrayOfObject.Length; i++) { writer.WriteByte((byte)charArrayOfObject[i]); } writer.Flush(); writer.Close(); mutex.ReleaseMutex(); }