使用FileSystemWatcher监视在同一实例上加载两个文件的时间 [英] Monitor when two files are loaded on the same instance using FileSystemWatcher

查看:73
本文介绍了使用FileSystemWatcher监视在同一实例上加载两个文件的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The following method is called when a file is open:

private void Load(string fileName)
{
//只观看seq文件。
this.watcher.Path = Path.GetDirectoryName(fileName);

//观察LastAccess和LastWrite时间的变化,以及
//重命名文件或目录。
this.watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
this.watcher.Filter = Path.GetFileName(fileName);

//开始观看。
this.watcher.EnableRaisingEvents = true;

//添加事件处理程序。
this.watcher.Changed + = this.OnChanged;

//异步打开文件的代码
}

private void OnChanged(对象源,FileSystemEventArgs e)
{
App.Current .Dispatcher.Invoke(()=>
{
MessageBoxResult result = CustomMessageBox.ShowYesNoCancel(
"该文件已被另一个实例修改。",
"打开更多实例!",
"覆盖",
"重新加载",
"另存为",
MessageBoxImage.Exclamation);

if(result == MessageBoxResult.Yes)
{
//覆盖
}

if(result == MessageBoxResult.Cancel)
{
//另存为;
}

 if (result == MessageBoxResult.No)
            {
                // reload file and lose changes

            }
        });
        var t = new System.Timers.Timer();
        ((FileSystemWatcher)source).Changed -= new FileSystemEventHandler(this.OnChanged);
        t.Interval = 1000;
        t.Elapsed += new ElapsedEventHandler(Elapsed);
        t.Start();
    }

    private static void Elapsed(object sender, ElapsedEventArgs e)
    {
        ((System.Timers.Timer)sender).Stop();
    }          

private static void Elapsed(object sender,ElapsedEventArgs e)
{
((System.Timers.Timer)sender).Stop();
}

1.我非常失望只有当保存一个实例的文件时我才得到我的MessageBox但我喜欢在第二个实例上加载文件时收到我的消息。主要问题是用户打开应用程序两次,现在可以在两个实例中进行修改。为了保存我正在使用这种方法:

1.I'm very disappointed that I get my MessageBox only when the file from one instances is saved but I like to have my message when I load the file on the second instance. Main problem is that the user opened the application twice and now is possible to modified in both instances. For saving I'm using this method:

public override void WriteStartObject(XmlDictionaryWriter writer,object graph);

public override void WriteStartObject(XmlDictionaryWriter writer, object graph);

这NotifyFilters.LastAccess工作吗?对我来说NotifyFilters.LastWrite似乎有效......

Is this NotifyFilters.LastAccess working? For me just NotifyFilters.LastWrite seems to work...

  1. 另一个小问题是,在一个实例上将更改保存到我的文件后,messageBox出现两次(好像我没有足够的问题)。




推荐答案

这些是从相应的文件系统属性反弹,因此与文件系统是否使用它们有关。上次访问时间更新是

禁用
,因此您不会收到通知除非你在NTFS中明确启用了这个功能,否则它们会再次出现。但这会对你的文件系统性能产生不利影响。

These are bouncing off the corresponding file system attributes and are therefore tied to whether the file system uses them. Last access time updates were disabled around the Vista timeframe so you won't get notified for them anymore unless you explicitly turned on this feature in NTFS. But this would have a detrimental impact on your file system performance.

FSW仅用于修改。要在文件出现时收到通知;由另一个进程打开你必须使用文件系统过滤器。当然这些不能用托管代码编写。

FSW is only for modifications. To get notified when a file is "opened" by another process you have to use a file system filter. Of course these cannot be written in managed code.

我想我已经在你之前的帖子中解决了#2。FSW加注它看到的每个更改的事件。这可能与保存应用程序的#相关,也可能不相关单个WriteFileEx调用可以触发多个更改通知。使用FSW时,您需要
来处理同一文件的多个事件。此外,您必须处理远程进程可能仍然打开文件以进行写访问这一事实,这意味着您甚至无法访问它。大多数人将通知推送到后台工作线程
,该线程在未来的某个时间点处理文件更改通知,以及重试和重复更改检测。这不是微不足道的。我几年前写过博客。 BonnieB最近和IIRC一起发表了关于它的博客。

I think I already addressed #2 in your previous post. FSW raises an event for each change it sees. That may or may not correlate to the # of saves an app did. A single WriteFileEx call can trigger multiple change notifications. When using the FSW you have to handle multiple events for the same file. Furthermore you have to handle the fact that the remote process may still have the file open for write access which means you cannot even access it. Most people push the notification to a background worker thread that processes the file change notification at a future point, along with retry and duplicate change detection. It isn't trivial. I've blogged about it years ago. BonnieB blogged about it recently as well IIRC.


这篇关于使用FileSystemWatcher监视在同一实例上加载两个文件的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆