2
私は2つのプロセス間で通信しようとしています。 MSDNのドキュメントから、私はMemoryMappingFileに出くわしました。私はこれを使って通信しています。MemoryMappingFileに関する問題
public class SmallCommunicator : ICommunicator
{
int length = 10000;
private MemoryMappedFile GetMemoryMapFile()
{
var security = new MemoryMappedFileSecurity();
security.SetAccessRule(
new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>("EVERYONE",
MemoryMappedFileRights.ReadWriteExecute, System.Security.AccessControl.AccessControlType.Allow));
var mmf = MemoryMappedFile.CreateOrOpen("InterPROC",
this.length,
MemoryMappedFileAccess.ReadWriteExecute,
MemoryMappedFileOptions.DelayAllocatePages,
security,
HandleInheritability.Inheritable);
return mmf;
}
#region ICommunicator Members
public T ReadEntry<T>(int index) where T : struct
{
var mf = this.GetMemoryMapFile();
using (mf)
{
int dsize = Marshal.SizeOf(typeof(T));
T dData;
int offset = dsize * index;
using (var accessor = mf.CreateViewAccessor(0, length))
{
accessor.Read(offset, out dData);
return dData;
}
}
}
public void WriteEntry<T>(T dData, int index) where T : struct
{
var mf = this.GetMemoryMapFile();
using (mf)
{
int dsize = Marshal.SizeOf(typeof(T));
int offset = dsize * index;
using (var accessor = mf.CreateViewAccessor(0, this.length))
{
accessor.Write(offset, ref dData);
}
}
}
#endregion
}
誰でもこのコードが機能しない理由を教えてください。同じコードをディスクファイルと一緒に使用すると動作します。
連続して読み書きすると、データが失われているように見えます。何か不足していますか?
誰もこの質問を見たことがないようです。それは...ですか ? – abhishek
[DelayAllocatePagesパラメータが一部のシステムで例外を引き起こしているようです](https://stackoverflow.com/questions/46083451/memorymappedviewstream-error-does-not-have-appropriate-access)という警告だけです。少量のメモリを扱っているので、おそらくそのパラメータは必要ないでしょう。 (ちょうどそれを前に割り当てる) また、Tは常に同じサイズですか?そうでない場合は、各項目のオフセットを追跡する必要があります。 – millejos