繁体   English   中英

Windows Service中的FileWatcher突然停止工作

[英]FileWatcher in Windows Service suddenly stops working

我正在Windows 2003中运行Windows服务。在上个月,每天都在向其中添加/更改代码,在上个月,我的FileWatcher一直在工作。 但是,由于某种奇怪的原因,它今天停止了工作。 如果我恢复为旧代码,它仍然不会引发事件。 我已经在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()方法的末尾。

尝试使用this.processfileWatcher.Created事件对其进行测试,并查看在创建新文件时它是否仍然触发。 另外,尝试手动设置NotifyFilter属性( doc ),看看是否可行。

如果某种原因导致大量磁盘快速更改,则在您有机会捕获所需内容之前,您可能已经用尽缓冲区,或者您的服务凭据可能不适用于您正在监视的内容。

尝试授予显式权限,然后通过为fileystemwatcher的error事件添加处理程序来查找缓冲区问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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