FileSystemWatcher events raising twice despite tak

2020-01-29 12:39发布

I've browsed quite a few threads on here and on other sites for solutions to this problem.

Here's my FileMonitor class:

class FileMonitor
{
    public FileMonitor(String path)
    {
        try
        {
            var watcher = new FileSystemWatcher()
            {
                Path = path,
                IncludeSubdirectories = true,
                InternalBufferSize = 65536,
                EnableRaisingEvents = true
            };

            watcher.Changed += new FileSystemEventHandler(OnFileChanged);
            watcher.Created += new FileSystemEventHandler(OnFileCreated);
            watcher.Deleted += new FileSystemEventHandler(OnFileDeleted);
            watcher.Renamed += new RenamedEventHandler(OnFileRenamed);
            watcher.Error += new ErrorEventHandler(OnWatcherError);
        }
        catch (Exception)
        {

            throw;
        }
    }

    private void OnWatcherError(object sender, ErrorEventArgs e)
    {

    }

    private void OnFileChanged(object sender, FileSystemEventArgs e)
    {
        try
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = false;

            LogFileSystemChanges(e);
        }

        finally
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = true;
        }
    }

    private void OnFileCreated(object sender, FileSystemEventArgs e)
    {
        try
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = false;

            LogFileSystemChanges(e);
        }

        finally
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = true;
        }
    }

    private void OnFileDeleted(object sender, FileSystemEventArgs e)
    {
        try
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = false;

            LogFileSystemChanges(e);
        }

        finally
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = true;
        }
    }

    private void OnFileRenamed(object sender, RenamedEventArgs e)
    {
        try
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = false;

            LogFileSystemRenaming(e);
        }

        finally
        {
            ((FileSystemWatcher)sender).EnableRaisingEvents = true;
        }
    }

    private void LogFileSystemChanges(FileSystemEventArgs e)
    {
        string log = string.Format("{0:G}: {1} | {2}", DateTime.Now, e.FullPath, e.ChangeType);
        Console.WriteLine(log);
    }

    private void LogFileSystemRenaming(RenamedEventArgs e)
    {
        string log = string.Format("{0:G}: {1} | Old name: {2}", DateTime.Now, e.FullPath, e.OldName);
        Console.WriteLine(log);
    }
}

As you can tell, I have tried the "lock" of ((FileSystemWatcher)sender).EnableRaisingEvents = false;, but I can tell from my console output my events are triggering twice.

Any ideas on this? I'm really not sure where to go from here.

2条回答
够拽才男人
2楼-- · 2020-01-29 13:25

I've tried to use the FileSystemWatcher class before (many years ago it turns out - 2008), and experienced major problems. It is a leaky abstraction at best. I reported my findings back then on CodeProject. Look for "Glytzhkof" in the comments list. As I recall I had problems with just about every aspect of the class, but it might have been improved now after so many years.

In summary my experience back in the day was that events disappeared altogether or piled up causing exceptions based on variables such as whether a disk's write cache was enabled, whether RAID, NAS or regular disks were accessed, and other random hardware issues that I do not recall exactly. Interestingly it seems it worked with UNC paths. No idea why. Mapped drives failed.

Have a look at the summary in the Code Project discussion. I am sure there are improvements since I tried it out, and a few new bugs perhaps :-). It seems disk storage hardware is hard to deal with as a high level abstraction - it leaks. Personally I ended up scanning for files manually using a service and regular disk functions. I wasted a lot of time before doing things manually.

UPDATE: See new info here: https://stackoverflow.com/a/23704476/129130

查看更多
Viruses.
3楼-- · 2020-01-29 13:26

I've had the same problem and after much ado came to the conclusion that Notepad writes a file three times (!) in row. If an app really saves that much often (maybe to triangulate backups and such) there isn't much you can do.

I also noticed you have attached to all events, you should narrow it down to the least you need and filter out those you aren't interested in with the NotifyFilters.

查看更多
登录 后发表回答