【发布时间】:2021-09-11 03:32:33
【问题描述】:
我创建了一个 Windows 服务,它使用 FileSystemWatcher 来查找不同目录中的更改。当我启动服务时出现错误:
Error 1053:The service did not respond to start or control request in timely fashion.
我认为错误是由于在Watch()方法中使用using语句导致的无限循环,如下所示:
public FileSystemWatcher Watch()
{
FileSystemWatcher watcher;
using (watcher = new FileSystemWatcher($"C:\\Users\\lashi\\AppData\\Roaming\\Sublime Text 3", _ext))
{
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.IncludeSubdirectories = true;
// Add event handlers.
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Renamed += OnRenamed;
// Begin watching.
watcher.EnableRaisingEvents = true;
}
return watcher;
}
这是我的 OnStart() 方法:
protected override void OnStart(string[] args)
{
String userName;
String expt;
if (args.Length < 2)
{
Console.WriteLine($"FileWatcher <user> <exptName>");
Console.WriteLine($"Captures files into /temp/<exptName>-log and /temp/<exptName>-files");
userName = "wost";
expt = "expt1";
}
else
{
userName = args[0];
expt = args[1];
}
String lexpt = $"C:\\Users\\lashi\\Desktop\\EMMC_CACHE\\{expt}-log";
String fexpt = $"C:\\Users\\lashi\\Desktop\\EMMC_CACHE\\{expt}-file";
if (!Directory.Exists(fexpt))
{
Directory.CreateDirectory(fexpt);
}
if (!Directory.Exists(lexpt))
{
Directory.CreateDirectory(lexpt);
}
// File Watcher Launch
Watcher w = new Watcher(lexpt, fexpt, userName);
FileSystemWatcher fw = w.Watch();
}
您能帮我找到解决此问题的方法吗?我尝试了很多建议,但它们似乎不起作用。谢谢!
【问题讨论】:
-
您的计算机中安装了该服务吗?如果是这样,您可以尝试使用 try catch 来实现异常。然后,您可以将其写入 log.txt 文件。然后就可以知道问题出在哪里了。
标签: c# visual-studio windows-services infinite-loop filesystemwatcher