안녕하세요
여기저기 질문글을 올려보지만 아는사람이 없는건지 답변이 없어서 지푸라기라도 잡는 심정으로 글을 써봅니다.
먼저 참고 코드는
https://www.codeproject.com/Articles/467600/Memory-Mapped-File-Interoperability-with-NET-Objec
이곳에서 참고하였으며
public void WriteObjectToMMF(string mmfFile, object objectData, string name)
{
// Convert .NET object to byte array
byte[] buffer = ObjectToByteArray(objectData);
// Create a new memory mapped file
// Create a SecurityIdentifier object for "everyone".
MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity();
CustomSecurity.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(name, buffer.Length, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, CustomSecurity, HandleInheritability.Inheritable);
// Create a view accessor into the file to accommmodate binary data size
using (MemoryMappedViewAccessor mmfWriter = mmf.CreateViewAccessor(0, buffer.Length))
{
Console.WriteLine(name);
mmfWriter.WriteArray<byte>(0, buffer, 0, buffer.Length);
}
}
이렇게 쓰는부분을 수정한 상태 입니다.
mmfFile 변수는 실제 쓰이고 있진 않지만 CreateFromFile 을 사용할떄 사용하기위해 임시로 넣어두었습니다.
현재 메모리에 1나의 name 만 넣고 뽑으면 잘 뽑힙니다.
하지만,
2개 이상 데이터를 등록시 문제가 생깁니다.
여러 문제중 발생 문제를 손꼽자면
WriteObjectToMMF(@"E:\DSME\GFS\windSpeedData.MMF", wsd, "windSpeedData");
WriteObjectToMMF(@"E:\DSME\GFS\windDirectionData.MMF", wdd, "windDirectionData");
이런식으로 Data를 넣고 read 하면 Write 를 마지막에 한놈만 뽑힙니다.
즉 SpeedData 는 경로가 잘못되었다고 뜹니다.
Read 시 MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(name,MemoryMappedFileRights.ReadWrite))
로 읽어 옵니다.
여기서 또 어처구니 없는건
write 를 버튼클릭 이벤트에 넣어주었는데. 프로그램 종료하지않고 write 가 들어있는 클릭이벤트를 연속으로 두번 눌렀을경우
wsd 의 데이터가 나오고 wdd 의 데이터는 나오지않습니다.
2개가 아닌 4개의 Data 를 입력시 write 의 위치에따라 어떤건 뽑히고 어떤건 안뽑히고 하는데
도저히 이유를 모르겠습니다.
도움좀 부탁 드리겠습니다.
[최초 등록일: ]
[최종 수정일: 9/23/2019]