多个内存映射的文件被错误吗?
当向我的项目添加第二个内存映射文件时,第一个内存映射文件不再跨进程工作。
Imports System.IO.MemoryMappedFiles
Module IPC
Private ReadOnly _QlCtxOpen As MemoryMappedViewAccessor = MemoryMappedFile.CreateOrOpen("IPC_QlCtxOpen", 1).CreateViewAccessor()
Public Property IsQlCtxOpen As Boolean
Get
Dim value As Boolean = _QlCtxOpen.ReadBoolean(0)
Debug.Print($"QlCtxOpen {value}")
Return value
End Get
Set(ByVal value As Boolean)
_QlCtxOpen.Write(0, value)
Debug.Print($"set QlCtxOpen to {value}")
End Set
End Property
End Module
当向模块添加以下行时,第一个 mmf 仍然在同一进程中工作,但第二个进程将不再能够读取正确的值。
Private ReadOnly _alreadyOpenPID As MemoryMappedViewAccessor = MemoryMappedFile.CreateOrOpen("IPC_alreadyOpenPID", 4).CreateViewAccessor()
我知道我应该使用互斥体进行写入,但在我的项目中,写入是由鼠标操作触发的,因此只有 1 个进程会同时使用它。
我不明白我做错了什么来破坏第一个 mmf,它无需第二个 mmf 即可工作。
我正在使用 Microsoft Visual Studio Community 2019 版本 16.11.11 .NET Framework 4.7.2
when adding a second memory mapped file to my project the first one no longer works cross processes.
Imports System.IO.MemoryMappedFiles
Module IPC
Private ReadOnly _QlCtxOpen As MemoryMappedViewAccessor = MemoryMappedFile.CreateOrOpen("IPC_QlCtxOpen", 1).CreateViewAccessor()
Public Property IsQlCtxOpen As Boolean
Get
Dim value As Boolean = _QlCtxOpen.ReadBoolean(0)
Debug.Print(quot;QlCtxOpen {value}")
Return value
End Get
Set(ByVal value As Boolean)
_QlCtxOpen.Write(0, value)
Debug.Print(quot;set QlCtxOpen to {value}")
End Set
End Property
End Module
when adding following line to the module the first mmf still works in the same process but a second process will no longer be able to read the correct value.
Private ReadOnly _alreadyOpenPID As MemoryMappedViewAccessor = MemoryMappedFile.CreateOrOpen("IPC_alreadyOpenPID", 4).CreateViewAccessor()
I know i should use a mutex for writing but in my project the write is triggered by mouse action so only 1 process will use it at the same time.
I don't understand what i'm doing wrong to break the first mmf, it works w/o the second mmf.
I'm using Microsoft Visual Studio Community 2019
Version 16.11.11 .NET Framework 4.7.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,垃圾收集是这里的罪魁祸首。
要修复此行为,只要您需要其他进程读取值,请确保在内存映射文件中引用对内存映射的文件。
为什么它仍然只能使用1个声明,而对我的内存映射文件没有引用对我来说是个谜。
Turns out garbage collection is the culprit here.
To fix this behavior make sure to have a reference to the memory-mapped file for as long as you need the value to be read by other processes.
Why it still worked with only 1 declaration and no reference to the memory-mapped file is a mystery to me.