Click or drag to resize

SafeFileWatcher Class

Represents a wrapper around the native .NET FileSystemWatcher that avoids problems with dangling references when using a file watcher instance as a class member that never gets disposed.
Inheritance Hierarchy
SystemObject
  GSF.IOSafeFileWatcher

Namespace: GSF.IO
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.205-beta+5ef4169f3b1079d8b163dd363614f656bd140924
Syntax
[SecurityCriticalAttribute]
public class SafeFileWatcher : IDisposable
View Source

The SafeFileWatcher type exposes the following members.

Constructors
 NameDescription
Public methodSafeFileWatcher Initializes a new instance of the SafeFileWatcher class.
Public methodSafeFileWatcher(String) Initializes a new instance of the SafeFileWatcher class, given the specified directory to monitor.
Public methodSafeFileWatcher(String, String) Initializes a new instance of the SafeFileWatcher class, given the specified directory and type of files to monitor.
Top
Properties
 NameDescription
Public propertyEnableRaisingEvents Gets or sets a value indicating whether the component is enabled.
Public propertyFilter Gets or sets the filter string used to determine what files are monitored in a directory.
Public propertyIncludeSubdirectories Gets or sets a value indicating whether subdirectories within the specified path should be monitored.
Public propertyInternalBufferSize Gets or sets the size of the internal buffer.
Public propertyNotifyFilter Gets or sets the type of changes to watch for.
Public propertyPath Gets or sets the path of the directory to watch.
Public propertySite Gets or sets an ISite for the SafeFileWatcher.
Public propertySynchronizingObject Gets or sets the object used to marshal the event handler calls issued as a result of a directory change.
Top
Methods
 NameDescription
Public methodBeginInit Begins the initialization of a SafeFileWatcher used on a form or used by another component. The initialization occurs at run time.
Public methodDispose Releases all the resources used by the SafeFileWatcher object.
Protected methodDispose(Boolean) Releases the unmanaged resources used by the SafeFileWatcher object and optionally releases the managed resources.
Public methodEndInit Ends the initialization of a SafeFileWatcher used on a form or used by another component. The initialization occurs at run time.
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalize Terminates SafeFileWatcher instance making sure to release unmanaged resources.
(Overrides ObjectFinalize)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Public methodWaitForChanged(WatcherChangeTypes) A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor.
Public methodWaitForChanged(WatcherChangeTypes, Int32) A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor and the time (in milliseconds) to wait before timing out.
Top
Events
 NameDescription
Public eventChanged Occurs when a file or directory in the specified Path is changed.
Public eventCreated Occurs when a file or directory in the specified Path is created.
Public eventDeleted Occurs when a file or directory in the specified Path is deleted.
Public eventError Occurs when the internal buffer overflows.
Public eventRenamed Occurs when a file or directory in the specified Path is renamed.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Remarks

The design goal of the SafeFileWatcher is to avoid accidental memory leaks caused by use of .NET's native file system watcher when used as a member of a class that consumers fail to properly dispose. If a class has a reference to a file watcher as a member variable and attaches to the file watcher's events, the file watcher will maintain a reference the parent class so it can call its event handler. If the parent class is not disposed properly, the file watcher will thusly not be disposed and will maintain the reference to the parent class - the garbage collector will never collect the parent because it has a valid reference and no collection means the parent finalizer will never get called and the file system watcher will never get disposed. Creating multiple instances of parent class and not disposing of them will cause a memory leak even with a properly designed disposable pattern. Using the SafeFileWatcher instead of directly using the FileSystemWatcher will resolve this potential issue.

Note that component model implementation is not fully replicated - if you are using a file system watcher on a design surface, this safety wrapper will usually not be needed. This class has benefit when a class will dynamically use a file watcher and needs to make sure any unmanaged resources get properly released even if a consumer neglects to call the dispose function.

See Also