[英]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.