我在 Windows 2003 中运行 Windows 服务。上个月我每天都在添加/更改代码,上个月我的文件观察器每天都在工作。但是,由于某些奇怪的原因,它今天停止工作。如果我恢复到旧代码,它仍然不会引发事件。我已经在我的 Win7 机器上测试了相同的代码,它工作正常。我假设有一个外部进程干扰,但我什至不确定要寻找什么。
以下是相关代码:
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() 方法已经结束。