我只需要处理一次

时间:2012-09-03 10:47:53

标签: c# events

我想写一个App来检查文本文件是否有变化。如果是的话,它会重新启动Windows服务。

我正在使用FileSystemWatcher,看起来它生成的事件很少。这是我的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceProcess;
using System.Threading;


namespace ConsoleApplication1
{
    class FileMon
    {
        public static bool status;

        public static void Run()
        {
          try
            {

                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = @"D:\test\";
                watcher.Filter = "*.txt";
                watcher.NotifyFilter = NotifyFilters.LastWrite;

                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.EnableRaisingEvents = true;

                Console.WriteLine(status);
                while (Console.Read() != 'q') ;

            }

            catch (Exception e)
            {
                Console.WriteLine(e);
            }

        }

        static void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine(e.ChangeType);
          //  status = true;
            Console.WriteLine(status);


            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            Console.WriteLine("before IF block status = {0} ", status);

            if (status == false)
            {
                RestartService();
                status = true;
                Console.WriteLine("after IF block status = {0} ", status);
            }

            else
            {

            }

        }


        public static void RestartService()
        {


            ServiceController service = new ServiceController("6to4");
            try
            {
                int millisec1 = Environment.TickCount;

                TimeSpan timeout = TimeSpan.FromMilliseconds(50);
                Console.WriteLine("trying to stop");

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

           //     Console.ReadKey();
                Console.WriteLine("stoping.........");

                // count the rest of the timeout
            //    int millisec2 = Environment.TickCount;
            //    timeout = TimeSpan.FromMilliseconds(50 - (millisec2 - millisec1));

                Console.WriteLine("dd");
              //  Console.ReadKey();

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);

            }
            catch
            {
                // ...
            }
        }

    }
}

当我运行它,并在文本编辑器中更改文本文件时,我得到下一个输出:

False
Changed
False
File: D:\test\1.txt Changed
before IF block status = False
trying to stop
after IF block status = True
Changed
True
File: D:\test\1.txt Changed
before IF block status = True
Changed
True
File: D:\test\1.txt Changed
before IF block status = True

它看起来像某个地方有bug,这就是为什么在单次更改后我看到的不是一个,而是三个副本的输出。我无法理解什么是错的,我错误的地方......

1 个答案:

答案 0 :(得分:0)

问题是FileSystemWatcher。它不是完美的防弹,有时会为单个更改生成多个事件,如果这些事件以高速率发生,也可能在更改后不会引发事件。

因此,您可以采取有关使用标记status的解决方法来对FileSystemWatcher的这些奇怪行为做出反应。