【发布时间】:2012-06-14 19:33:16
【问题描述】:
我在 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() 方法已经结束。
【问题讨论】:
-
您在哪里设置 FileSystemWatcher 的 Path 属性?
-
在类的构造函数中使用 /onstart/onstop 方法: public Ex() { Settings = new Settings();初始化组件(); processfileWatcher.Path = Settings.ProcessFolder; }
-
我不知道为什么它不适合你,但这里有一些通用代码建议:1)你不需要将你的
FileSystemWatcher转换为FileSystemWatcher2 ) 在初始化代码中设置 Path 字段可能是个好主意。 -
在您的 OnStart 中放置一个 Debugger.Launch,然后您可以附加调试器并遍历代码。您可能会看到某些行未命中或被吞没的异常。
-
Debugger.Launch 正在使我的服务崩溃...我错过了什么吗?
标签: c# service filesystemwatcher