FileSystemWatcherChanged 事件不会为内存映射文件触发

FileSystemWatcherChanged event dosen't fire for a memory mapped file

我有 json 个文件要在两个进程之间共享。所以我创建了一个内存映射文件如下。

private void CreateMemoryMappedFile()
        {
            var info = Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/" + model.Data.Settings.OrcaUISpecificSettings.TimeOutFolder);
            string path = Path.Combine(info.FullName + @"\" + model.Data.Settings.OrcaUISpecificSettings.File);
            FullPath = path;
            try
            {
                mmf = MemoryMappedFile.CreateFromFile(path, FileMode.CreateNew, "MyMemoryFile", 1024 * 1024);
            }
            catch (Exception ex)
            {

            }
        }

        public MemoryMappedViewStream GetAccessor()
        {
            MemoryMappedViewStream FileMapView = null;
            if (FileMapView != null)
            {
                return FileMapView;
            }
            FileMapView = mmf.CreateViewStream();
            return FileMapView;
        }

为了读取和写入文件,我正在执行以下操作

public void WriteToMemoryMappedFile(string Data)
        {
            try
            {
                mutex.WaitOne();
                byte[] bytes = Encoding.ASCII.GetBytes(Data);
                var accessor = GetAccessor();

                accessor.Write(bytes, 0, Data.Length);
                mutex.ReleaseMutex();
            }
            catch (Exception ex)
            {

            }
        }

public string ReadFromMemoryMappedFile()
        {
            mutex.WaitOne();
            var accessor = GetAccessor();
            using (BinaryReader binReader = new BinaryReader(accessor))
            {
                byte[] reader = binReader.ReadBytes((int)accessor.Length);
                string result = Encoding.Default.GetString(reader);
                mutex.ReleaseMutex();
                return result.Replace("NULL", "");
            }
        }

我的问题是我的应用程序有一个 Activity 监视器。所以在 x 时间后我用 InActiveStatus 更新 json 文件。同样,我正在监听任何文件更改(查看 D_IDle 事件)。问题是如果更改了普通文件,我会很好地触发 FileSytemWatcher 更改事件。但是,当我使用内存映射文件更新状态时,FileSystemWatcher 更改事件永远不会被触发,请帮忙。

private void D_IsIdle(object sender, EventArgs e)
        {
            MonitorDirectory();
            //AppViewModel.SerializeData("InActive");
            AppViewModel.SerializeDataToMemoryMap("InActive");
            d.IsIdle -= D_IsIdle;
        }
public void MonitorDirectory()
        {
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(AppViewModel.GetDriectory());
            fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
            fileSystemWatcher.Filter = "*.json";
            fileSystemWatcher.Changed += FileSystemWatcher_Changed;
            fileSystemWatcher.EnableRaisingEvents = true;
        }
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
//IT NEVER COMES HERE
}

将 FileSystemWatcher 与内存映射文件一起使用一定是某种反模式:)。如果您在本地系统上,请使用进程同步原语之一(例如信号量)来发出更改信号。

我的猜测是当文件句柄关闭时 FileSystemWatcher 触发以避免其他处理读取部分写入的文件。