C#FileSystemWatcher的WaitForChanged方法只能检测一个文件的变化 [英] C# FileSystemWatcher WaitForChanged Method only detects one file change

查看:1061
本文介绍了C#FileSystemWatcher的WaitForChanged方法只能检测一个文件的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了类似这样的一个问题: FileSystemWatcher的 - 只有改变事件一旦触发一次?

I got a problem similar to this one: FileSystemWatcher - only the change event once firing once?

但由于该线程是两岁和我的代码是有点不同,我决定开一个新的问题。

But since that thread is two years old and my code is a bit different, I decided to open a new question.

嗯,这里是我的代码:

while (true)
{
  FileSystemWatcher fw = new FileSystemWatcher();

  fw.Path = @"Z:\";
  fw.Filter = "*.ini";
  fw.WaitForChanged(WatcherChangeTypes.All);
  Console.WriteLine("File changed, starting script...");
  //if cleanup
  try
  {
      if (File.ReadAllLines(@"Z:\file.ini")[2] == "cleanup")
      {
          Console.WriteLine("Cleaning up...");
          Process c = new Process();
          c.StartInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim('\\') + @"\clean.exe";
          c.StartInfo.WorkingDirectory = System.Environment.SpecialFolder.DesktopDirectory.ToString();
          c.Start();
          c.WaitForExit();
          Console.WriteLine("Done with cleaning up, now starting script...");
      }
  }
  catch
  {
      Console.WriteLine("No cleanup parameter found.");
  }
  Process p = new Process();
  p.StartInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim('\\') + @"\go.exe";
  p.StartInfo.WorkingDirectory = System.Environment.SpecialFolder.DesktopDirectory.ToString();
  p.Start();
  Console.WriteLine("Script running...");
  p.WaitForExit();
  fw = null;
  Console.WriteLine("Done. Waiting for next filechange...");
}



问题:这个程序应检测文件中的文件更改Z:\\ \\file.ini。如果它的变化,脚本应该被解雇。当剧本完成后,应PROGRAMM返回到开始和开始观看更改,再次(这就是为什么我用while循环)。
哦,当检测到第一个变化,似乎一切都工作得很好,但在第一个的任何变化都不会被检测到。我试图FileSystemWatcher的对象设置为null,你可以看到,但它并没有帮助。

Problem: This program should detect a file change in the file "Z:\file.ini". If it has changed, a script should be fired. When the script is done, the programm should return to the start and start watching for changes, again (that's why I used the while-loop). Well, the first change is detected and everything seems to be working just fine, but any changes AFTER the first one are not going to be detected. I tried to set the FileSystemWatcher Object to null, as you can see, but it didn't help.

所以,我希望好的答案。谢谢你。

So, I hope for good answers. Thanks.

推荐答案

我会改变你的设计让你不依靠FileSystemWatcher的任何变化。相反轮询目录或你看任何改变的文件。然后,您可以结合使用FileSystemWatcher的与此有关,尽快唤醒它,如果我们知道有变化。这样,如果你错过了一个事件,你还是从它基于您的调查超时恢复。

I would change your design so you don't rely on the FileSystemWatcher for any changes. Instead poll the directory or the file that your watching for any changes. You can then use the FileSystemWatcher in conjunction with this to wake it up as soon as possible if we know there are changes. This way, if you miss an event, you'd still recover from it based on your poll time-out.

例如

    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(@"f:\");
        ManualResetEvent workToDo = new ManualResetEvent(false);
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Changed += (source, e) => { workToDo.Set(); };
        watcher.Created += (source, e) => { workToDo.Set(); };

        // begin watching
        watcher.EnableRaisingEvents = true;

        while (true)
        {
            if (workToDo.WaitOne())
            {
                workToDo.Reset();
                Console.WriteLine("Woken up, something has changed.");
            }
            else
                Console.WriteLine("Timed-out, check if there is any file changed anyway, in case we missed a signal");

            foreach (var file in Directory.EnumerateFiles(@"f:\")) 
                Console.WriteLine("Do your work here");
        }
    }

这篇关于C#FileSystemWatcher的WaitForChanged方法只能检测一个文件的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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