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?
In the example above
FileSystemWatcher
is kept alive only because the propertyEnableRaisingEvents
is set totrue
. The fact that the Singleton class has an event handler registered toFileSystemWatcher.Changed
event does not have any direct bearing onfsw
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 tofalse
, theFileSystemWatcher
object is garbage collected: OnceGC.Collect()
is called, theIsAlive
property on theWeakReference
isfalse
.