N0xus
N0xus

Reputation: 2724

C# FileSystemWatcher not triggering correctly in Service

I'm working on a service where in my OnStartmethod I have the following lines of code to set up my FileSystemWatcher

Log.Info($"File location {_location}");
var watcher = new FileSystemWatcher(_location);      
watcher.Changed += new FileSystemEventHandler(OnChanged);

Then in my OnChanged method I am wanting to start a timer like so:

private void OnChanged(object source, FileSystemEventArgs e)
{
    Log.Info($"A file has been placed in {_location} starting timer");
    OnTimer(null, null); //run immediately at startup
    StartEventTimer();
}

The timer code works, so I know that isn't an issue, likewise in my log I know it is checking for the correct location. What is it that I'm missing?

All I'm wanting my code to do is to trigger my timer, the moment a file is placed in my target location yet I've not been able to do so. Am I correct in that I should be using FileSystemWatcherto do this, or should I use something else as this code is within a service?

Upvotes: 2

Views: 1757

Answers (2)

DiskJunky
DiskJunky

Reputation: 4971

There are a couple of things it could be based on what you said there.

The first thing of note is that the declaration for var watcher looks like it's not a class variable and will go out of scope when it exits OnStart(). You'll need to move the declaration outside of that.

The second item of interest is that it looks like EnableRaisingEvents isn't being set. A working example of the FileSystemWatcher is below.

public class SomeService
{
    private FileSystemWatcher _watcher;
    public void OnStart()
    {
        // set up the watcher
        _watcher = new FileSystemWatcher(_location);
        _watcher.Path = path;
        _watcher.NotifyFilter = NotifyFilters.LastWrite;
        _watcher.Filter = "*.*";
        _watcher.Changed += new FileSystemEventHandler(OnChanged);
        _watcher.EnableRaisingEvents = true;
    }
}

EDIT

As Ben Hall mentioned, it is possible that multiple events can be raised for the same file when a file is moved into the folder. As per the MSDN documentation;

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher

Upvotes: 0

Ben Hall
Ben Hall

Reputation: 1423

You might well find that the Changed event is firing more than once on a new file, a common problem with some applications, which might create unwanted side effects later on. Have a look and try changing to Created instead.

If you're looking for a new file appearing in a folder, you should use:

watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Created += OnCreated;

Gist demonstrating it firing twice using Changed on LastWrite and a for predictable behaviour, a Gist demonstrating single fire on file create using Created and NotifyFilter.FileName

Just run it up in a Console App and copy a file into c:\temp.

Upvotes: 1

Related Questions