我运行Windows服务在Windows 2003中我已经添加/每天都在变化的代码给它的最后一个月,和日常上个月我filewatcher一直在努力。但是,由于某种奇怪的原因,它今天停止工作。如果我恢复到旧代码,它仍然不会引发事件。我在我的Win7机器上测试过相同的代码,它工作正常。我假设有一个外部过程干扰,但我甚至不知道要寻找什么。FileWatcher在Windows服务突然停止工作
下面是相关代码:
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;
}
}
通过调试语句,我可以确认我达到我在onStart()方法的末尾。
你在哪里设置你FileSystemWatcher的的路径属性? –
在具有/ onstart/onstop方法的类的构造函数中:public Ex() { Settings = new Settings(); InitializeComponent(); processfileWatcher.Path = Settings.ProcessFolder; } – user1287523
我不知道为什么它不为你工作,但是这里有几个通用代码的建议:1)你不需要投你的'FileSystemWatcher'到'FileSystemWatcher' 2)它可能是一个在Init代码中设置Path字段是个好主意。 –