[英]MemoryMappedFiles: persistance and inter process communication
I have an issue with memorymappedfiles. 我的memorymappedfiles有问题。
1) Each time I request a file (with the same name), I seem to get a new file. 1)每次我请求一个文件(具有相同的名称)时,我似乎都会得到一个新文件。 The bytes I wrote are not present on next access.
我写的字节在下次访问时不存在。 I only managed to fix that by persisting the object, which I thought should not be needed.
我只能通过持久化对象来解决此问题,我认为该对象是不需要的。
2) As for inter process communication, I also always seem to get 2 different mmf objects for the 2 processes I have, at least I don't see the changes of the other process. 2)至于进程间的通信,我似乎总是为我拥有的2个进程得到2个不同的mmf对象,至少我看不到其他进程的变化。
The FileName is both the same between the 2 processes and also it stays the same between successive calls. 两个进程之间的FileName相同,并且在后续调用之间也保持相同。
The code is slightly modified off http://www.abhisheksur.com/2012/02/inter-process-communication-using.html . 该代码已在http://www.abhisheksur.com/2012/02/inter-process-communication-using.html上稍作修改。
The "read" parameter and the code behind it in the if condition also does not change anything for the better. if条件中的“ read”参数及其后面的代码也不会发生任何变化。
MemoryMappedFile file = null;
private MemoryMappedFile GetMemoryMapFile(bool read)
{
if (file != null)
return file;
var security = new MemoryMappedFileSecurity();
var everyone = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
// https://stackoverflow.com/a/5398398/586754
// everyone not present in german version..
security.SetAccessRule(
new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>(everyone,
MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
MemoryMappedFile mmf;
if (read)
mmf = MemoryMappedFile.OpenExisting(FileName,
MemoryMappedFileRights.Read, //.ReadWriteExecute,
HandleInheritability.Inheritable);
else
mmf = MemoryMappedFile.CreateOrOpen(FileName,
this.length,
MemoryMappedFileAccess.ReadWrite,
MemoryMappedFileOptions.None,
security,
HandleInheritability.Inheritable);
file = mmf;
return mmf;
}
public Transfer ReadEntry()
{
try
{
var mf = this.GetMemoryMapFile(read: true);
byte[] arr = new byte[length];
int offset = 0;
using (var accessor = mf.CreateViewAccessor(0, length))
{
accessor.ReadArray(offset, arr, offset, length);
}
var str = System.Text.Encoding.UTF8.GetString(arr, 0, length);
return Serializer.DeserializeFromText<Transfer>(str);
}
catch (Exception)
{
return new Transfer();
}
}
public void WriteEntry(Transfer entry)
{
try
{
var mf = this.GetMemoryMapFile(read: false);
int offset = 0;
var str = Serializer.SerializeToText(entry);
byte[] arr = System.Text.Encoding.UTF8.GetBytes(str);
using (var accessor = mf.CreateViewAccessor(0, this.length))
{
accessor.WriteArray(offset, arr, offset, arr.Length);
}
}
catch { }
}
Edit: see answer, the main problem was with the 2 processes being a service and a user app, so there are slightly different rules. 编辑:参见答案,主要问题在于两个进程分别是服务和用户应用程序,因此规则略有不同。
Also, I am not sure if I really need the "complicated" GetMMF version or just a simplre one like 另外,我不确定我是否真的需要“复杂的” GetMMF版本或只是简单的版本,例如
private MemoryMappedFile GetMemoryMapFile()
{
var mmf = MemoryMappedFile.CreateOrOpen(FileName,
this.length);
return mmf;
}
I never fully understood the problem.. I am sharing an MMF between a service app and a user app. 我从未完全理解问题。我正在服务应用程序和用户应用程序之间共享MMF。 So that is the issue.
这就是问题所在。
I guess I did not have to change much more than naming the file "global\\myfile" rather than "myfile". 我想除了命名文件“ global \\ myfile”而不是“ myfile”,我所做的更改不必太多。
See also https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/ebbc9cc3-03a4-4111-a157-fc5777929acb/shared-memory-between-a-service-and-a-user-app-in-windows-7?forum=windowssdk&prof=required for some more details (thoug this is C++ based). 另请参阅https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/ebbc9cc3-03a4-4111-a157-fc5777929acb/shared-memory-between-a-service-and-a-user-app- in-windows-7?forum = windowssdk&prof =需要更多详细信息(这是基于C ++的)。
I will see to add the service tag to original question, just to make it clearer for future readers. 我将看到将服务标签添加到原始问题,以使将来的读者更加清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.