简体   繁体   中英

FileWatcher in Windows Service suddenly stops working

I'm running a windows service in Windows 2003. I've been adding/changing code to it every day for the last month, and everyday for the last month my filewatcher has been working. However, it stopped working today for some odd reason. If I revert back to old code, it still won't raise events. I've tested the same code on my Win7 machine and it is working fine. I'm assuming there is an external process interfering but I'm not even sure what to look for.

Here is the relevant code:

private void InitializeComponent()
    {
        this.processfileWatcher = new System.IO.FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize) (this.processfileWatcher)).BeginInit();
        this.processfileWatcher.EnableRaisingEvents = true;
        this.processfileWatcher.Filter = "done.txt";
        this.processfileWatcher.Changed += new System.IO.FileSystemEventHandler(this.processfileWatcher_Changed);
        this.ServiceName = "Service1";
        ((System.ComponentModel.ISupportInitialize)(this.processfileWatcher)).EndInit();

    }

private void processfileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        try
        {
            processfileWatcher.EnableRaisingEvents = false;
            //Do stuff here
            Debug.WriteLine(" End of processfileWatcher for: " + e.FullPath);
        }

        finally
        {
            processfileWatcher.EnableRaisingEvents = true;
        }

    }

Through debug statements I can confirm that I am reaching the end of my onStart() method.

Try testing it with this.processfileWatcher.Created event and see if it still raises when new files are created. Also try setting the NotifyFilter property manually ( doc ) and see if that works.

If something is causing a lot of rapid disk changes you may be running out of buffer before you have the chance to catch the thing you are looking for, or your service's credentials may not be working for what you are monitoring.

Try granting explicit permissions then look for the buffer issue by adding a handler for the fileystemwatcher's error event.

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.error%28v=vs.100%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM