Do I need to keep a reference to a FileSystemWatcher?

吃可爱长大的小学妹 提交于 2019-12-21 09:15:15

问题


I'm using a FileSystemWatcher (in an ASP.NET web app) to monitor a file for changes. The watcher is set up in the constructor of a Singleton class, e.g:

private SingletonConstructor()
{
    var fileToWatch = "{absolute path to file}";
    var fsw = new FileSystemWatcher(
        Path.GetDirectoryName(fileToWatch),
        Path.GetFileName(fileToWatch));
    fsw.Changed += OnFileChanged;
    fsw.EnableRaisingEvents = true;
}

private void OnFileChanged(object sender, FileSystemEventArgs e)
{
    // process file...
}

Everything works fine so far. But my question is:

Is it safe to setup the watcher using a local variable (var fsw)? Or should I keep a reference to it in a private field to prevent it from being garbage collected?


回答1:


In the example above FileSystemWatcher is kept alive only because the property EnableRaisingEvents is set to true. The fact that the Singleton class has an event handler registered to FileSystemWatcher.Changed event does not have any direct bearing on fsw being eligible for Garbage collection. See Do event handlers stop garbage collection from occurring? for more information.

The following code shows that with EnableRaisingEvents set to false, the FileSystemWatcher object is garbage collected: Once GC.Collect() is called, the IsAlive property on the WeakReference is false.

class MyClass
{
    public WeakReference FileSystemWatcherWeakReference;
    public MyClass()
    {
        var fileToWatch = @"d:\temp\test.txt";
        var fsw = new FileSystemWatcher(
            Path.GetDirectoryName(fileToWatch),
            Path.GetFileName(fileToWatch));
        fsw.Changed += OnFileChanged;
        fsw.EnableRaisingEvents = false;
        FileSystemWatcherWeakReference = new WeakReference(fsw);
    }

    private void OnFileChanged(object sender, FileSystemEventArgs e)
    {
        // process file... 
    }

}

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();
        GC.Collect();
        Console.WriteLine(mc.FileSystemWatcherWeakReference.IsAlive);
    }
}


来源:https://stackoverflow.com/questions/9110617/do-i-need-to-keep-a-reference-to-a-filesystemwatcher

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