将 FileSystemWatcher 和 Timer 添加到 .Net 2.0 Windows 服务 [英] Add FileSystemWatcher and Timer to a .Net 2.0 windows service
问题描述
任何人都看到添加 FileSystemWatcher 和 Timer 的任何潜在问题进入我的派生 ServiceBase 类??
Anyone see any potential problems adding a FileSystemWatcher and a Timer into my derived ServiceBase class??
马尔科姆
推荐答案
请务必将每个事件的事件包装在 try-catch 中,因为它们将在线程池中的线程上运行.如果在处理该事件期间出现异常,您的服务可能会意外停止运行.您还应该尽量减少 FileSystemWatcher 事件中的处理,例如,只需将文件路径添加到队列中,然后让您的计时器或后台工作程序之一处理队列.
Just be sure to wrap the events for each in a try-catch as they will run on threads from the Thread Pool. If there is an exception during the processing of that event your service may stop running unexpectedly. You should also keep the processing in the FileSystemWatcher Event to a minimum, for instance, just add the file path to a queue and then have one of your timers or Background Worker process the queue.
这篇关于将 FileSystemWatcher 和 Timer 添加到 .Net 2.0 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!