devklick
devklick

Reputation: 2619

FileSystemWatcher "wrapper" not triggering events

I've created a wrapper class for the FileSystemWatcher, but I cannot get the events to fire. They fire as expected when using the FileSystemWatcher class, but not my wrapper.

Any ideas why?

public class FolderWatcher
{
    public event FileSystemEventHandler Created;
    public event FileSystemEventHandler Changed;
    private FileSystemWatcher _watcher;

    public FolderWatcher(string watchedFolderPath)
    {
        _watcher = new FileSystemWatcher(watchedFolderPath);
        _watcher.Created += Created;
        _watcher.Changed += Changed;
        _watcher.EnableRaisingEvents = true;
    }
}

class Program
{
    static async Task Main(string[] args)
    {
        string watchedFolder = "/home/user";

        // 1. Custom wrapper for FileSystemWatcher. No events get fired.
        FolderWatcher watcher = new FolderWatcher(watchedFolder);

        // 2. The built in FileSystemWatcher works as expected. Uncomment to test.
        // FileSystemWatcher watcher = new FileSystemWatcher(watchedFolder);
        // watcher.EnableRaisingEvents = true;

        bool createdTriggered = false;
        bool changedTriggered = false;
        bool wait = (changedTriggered == false && createdTriggered == false);

        watcher.Created += (sender, args) => 
        {
            createdTriggered = true;
            Console.WriteLine($"{args.Name} created!");
        };

        watcher.Changed += (sender, args) => 
        {
            changedTriggered = true;
            Console.WriteLine($"{args.Name} changed!");
        };

        Console.WriteLine($"Watching folder {watchedFolder}");

        while (wait)
        {
            await Task.Delay(1);
        }

        Console.WriteLine("Closing application...");
    }
}

To test this, I've just been firing it up and creating a new file/editing an existing file in /home/user. Scenario 1 (annotated in the code) doesnt fire the events, but scenario 2 (annotated in the code), does fire the events. I'm struggling to work out why.

Worth noting that I'm loosely following this answer.

Upvotes: 0

Views: 291

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180787

Instead of trying to hook the File Watcher's events to yours, simply fire your events when you capture the File Watcher's events.

watcher.Changed += (sender, args) => 
{
    if (Changed != null)
        Changed(sender, args);
}

Your events are there for someone using your class to hook.

Upvotes: 2

sspaniel
sspaniel

Reputation: 677

To simplify Robert Harvey answer, just update your constructor to:

    public FolderWatcher(string watchedFolderPath)
    {
        _watcher = new FileSystemWatcher(watchedFolderPath);

        _watcher.Created += (sender, args) =>
        {
            if (this.Created != null)
            {
                this.Created(sender, args);
            }
        };

        _watcher.Changed += (sender, args) =>
        {
            if (this.Changed != null)
            {
                this.Changed(sender, args);
            }
        };

        _watcher.EnableRaisingEvents = true;
    }

Upvotes: 0

Related Questions