FileSystemWatcher stops raising events after a period of time

你说的曾经没有我的故事 提交于 2019-12-21 05:37:24

问题


We have built a window service that listens to folders with FileSystemWatcher, when created we process the file and so on. But after couple of days the event stops working.

  • Is it possible that it being collected by the garbage collector (GC)?
  • Does the GC collect it holding class (which is a singleton)?
  • Should I use a weak event?
  • Do I have a bug that means the event gets unregistered?

What i think the problem is, that FSW has an internal buffer, when it overflows its an error, take a look in this article that offer code to solve this problem.
Hope this help anyone.


回答1:


A couple of things to try, as suggested here:

  • In the timer tick event handler, stop your timer before doing your tick event handling, and restart it before exiting the handler
  • Wrap your timer code in a try/catch block to see what's happening.
  • Read the Elapsed event of the System.Timers.Timer class is not raised in a Windows service bug description/fix and see if that helps you.

In summary:

protected virtual void TimerTick(object sender, EventArgs e)
{
    // stop your timer
    this.timer.Stop();

    try
    {
        // TODO: add event handler specifics
    }
    catch
    {
        // TODO: add some logging to help you see what's going on
    }        

    // restart your timer
    this.timer.Start();
}


来源:https://stackoverflow.com/questions/6671652/filesystemwatcher-stops-raising-events-after-a-period-of-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!