通过使用filesystemwater来检测共享文件夹中的新文件,检测新文件将花费很长时间。该应用程序已经运行了两年,没有问题。从今年7月开始,它开始出现问题。没有任何代码更改。
基于应用程序日志,应用程序将在30秒之前检测到共享文件夹中的文件。现在开始需要3分钟到8个小时。它真的是有线的。似乎文件检测在某处被阻止,如果我重新启动服务,它可以触发检测。
public BRWatcher(string folderPath)
{
this.folderPath = folderPath;
this.watcher = new FileSystemWatcher(folderPath);
//Watch for LastWrite times
this.watcher.NotifyFilter = NotifyFilters.LastWrite;
// Add event handlers.
this.watcher.Changed += new FileSystemEventHandler(OnChanged);
this.watcher.Created += new FileSystemEventHandler(OnChanged);
this.watcher.Deleted += new FileSystemEventHandler(OnDeleted);
this.watcher.Error += new ErrorEventHandler(DealWithError);
//cache list: To prevent multiple triggers on the same arrival of a batch report, clear it when the service is restarted.
this.cacheList = new Dictionary<string,string>();
}
protected void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
//Ignore the first trigger by creating the new folder
if (Directory.EnumerateFiles(e.FullPath).Count()==0) return;
//Ignore the folder name starting with TEST
if (e.Name.StartsWith("TEST")) return;
//Ignore all the coming files in first level of the sharefolder
if (ComFunc.IsFolder(e.FullPath))
{
// new folder created
if (e.ChangeType.ToString().Equals(SystemInfoDefinition.FOLDER_CREATED))
{
//Do nothing
}
// batch report created or updated
else if (e.ChangeType.ToString().Equals(SystemInfoDefinition.FOLDER_CHANGED))
{
BRUploaderEvents.Log.MonitorNewBatchReportDetected(e.FullPath, e.Name);
// Updated batch report file
if (!this.cacheList.Keys.Contains(e.Name))
{
string folderModifiedDate = this.GetModifiedDate(e.FullPath);
this.cacheList.Add(e.Name,folderModifiedDate);
this.UploadBatchReport(e.FullPath, e.Name);
}
else
{
if (this.GetModifiedDate(e.FullPath) == this.cacheList[e.Name])
{
return;
}
else
{
this.UploadBatchReport(e.FullPath, e.Name);
}
}
}
}
}
catch(Exception ex){
// log the application error and send the exceptions to administrator
BRUploaderEvents.Log.ApplicationError(ex.Message, "Folder monitor - OnChange event");
SupporterEmail.SendUnexpectedErrorEmail(ex);
//remove current
if (this.cacheList.Keys.Contains(e.Name))
{
this.cacheList.Remove(e.Name);
}
}
}
有人遇到与我相同的问题吗?任何网络安全设置都会影响FileSystemWatcher的工作吗?期待收到一些好主意。预先感谢。