-3

FileSystemWatcher does not work properly. It only responds when the first change occurs. If I then change a second file, nothing happens.

public class ImageViewModel : INotifyPropertyChanged
{
    public static ImageViewModel singletonInstance { get; set; }

    FileSystemWatcher watcher;
    private readonly BackgroundWorker worker1;

    public ImageViewModel()
    {
        ...

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

        this.worker1 = new BackgroundWorker();
        this.worker1.DoWork += this.DoWork1;
        this.worker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker1_Completed);
    }

    ...

    private void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        editedFile = e.FullPath;

        if (worker.IsBusy == true || worker1.IsBusy == true)
        {
            autoEvent.WaitOne();
        }

        else
        {
            this.worker1.RunWorkerAsync();
        }
    }
}

Can you help me solve this problem?

Guilian
  • 129
  • 1
  • 11
  • 1
    it seems odd your watcher only works once.. are you sure you dont disable it in the watcher_changed ? – BugFinder Dec 08 '16 at 09:31
  • 2
    What is the error? *does not work properly* doesn't really help us. –  Dec 08 '16 at 09:31
  • 3
    What has the BackgroundWorker got to do with this? Why is the `watcher` field not `private readonly`,like `worker1`? What are you doing in `watcher_Changed`? Please provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – Clemens Dec 08 '16 at 09:33
  • does not work properly -> watcher only works once! – Guilian Dec 08 '16 at 09:37
  • Can you post the code of `DoWork1`? – Peter Bons Dec 08 '16 at 09:55

1 Answers1

1

The watcher_Changed event handler won't be invoked again until you signal by calling Set() method of the AutoResetEvent. The following call will block the UI thread and while it is blocked it cannot handle any events:

autoEvent.WaitOne();

If you temporarily remove all your code from the watcher_Changed event handler and just set a breakpoint in there and debug your application you should see that it actually gets hit for each file change:

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    int d = 1; // set a breakpoint on this line, debug your application and modify the file
}

But please remember to always post a a minimal, compilable and runnable sample of your issue.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I used `autoEvent.WaitOne()` because I use two BackgroundWorkers and they should not run at the same time. Before a BackgroundWorker beginns to accomplish anything it should always wait for the other to complete its works and then starts automatically. How can I realize that??? – Guilian Dec 09 '16 at 15:57
  • That's another question really. But you must call autoReset.Set() from somewhere for the UI thread to be able to process the events. You could for example do this in an event handler for the BackgroundWorker's RunWorkerCompleted event. But please ask a new question if you have another issue. – mm8 Dec 09 '16 at 16:14