저는 C#으로 작성한 Windows 서비스를 사용하고 있습니다. 그 뒤에는 FileSystemWatcher가 있습니다. FSW는 새 파일을 찾아 이에 따라 처리합니다. 서비스가 시작되면 기존 파일을 처리해야합니다. 콘솔 앱을 통해이 작업을 수행하면 모든 것이 예상대로 작동합니다.간단한 C# FileSystemWatcher Windows 서비스를 빠르게 시작하십시오.
그러나이 모든 것을 Windows 서비스에서 랩하려고 시도하면 내 첫 번째 문제는 Windows 서비스가 시작되지 않는다는 것입니다. 처음에는 많은 파일이 처리 되더라도 처리하는 데 너무 오래 걸렸기 때문에 시간이 초과되었습니다.
public WatcherService()
{
_log.Debug("WatcherService instantiated.");
_watcher = new FileSystemWatcher { Path = AppConfig.MonitorFolder, IncludeSubdirectories = true };
// we want the watching to start BEFORE we process existing files
// because if we do it the other way, a file might get missed
_watcher.Created += File_OnChanged;
}
public void StartWatching()
{
_log.Debug("WatcherService started.");
// this kicks off the watching
_watcher.EnableRaisingEvents = true;
// process existing files
ProcessExistingFiles(AppConfig.MonitorFolder);
}
내 해결 방법은 FSW "보고"와 별도의 비동기 스레드에서 초기 파일의 처리와 같은 킥오프했다 : 여기
내 "보고"클래스에 대한 코드의 일부입니다 이 (내 Windows 서비스 코드) :protected override void OnStart(string[] args)
{
_log.Debug("LoggingService starting.");
// kick off the watcher on another thread so that the OnStart() returns faster;
// otherwise it will hang if there are a lot of files that need to be processed immediately
Task.Factory.StartNew(() => _watcher.StartWatching()).ContinueWith(t =>
{
if (t.Status == TaskStatus.Faulted)
{
_log.Error("Logging service failed to start.", t.Exception.InnerException ?? t.Exception);
}
});
}
나는 Task.Factory.StartNew (에서 "StartWatching"방법)에 ONSTART()가 이해할 수 있도록, 타임 아웃 것을 포장하지 않은 경우. 하지만 이제는 StartWatching() 메서드가 호출되지 않습니다. 로그에 "LoggingService starting"이 표시되지만 "WatcherService started"는 표시되지 않습니다. (편집 : 참고로, Task.Run()도 시도했지만, 아무 소용이 없습니다.)
그게 뭐야? StartNew()가 수행하는 작업을 이해하지 못하거나 내가 성취하려는 작업을 수행하는 것이 더 좋습니다.
생각하십니까?
감사합니다.
다른 조건문을 사용하고 출력 결과를 확인해보십시오 (아직 시도하지 않은 경우). –