Gemstone Common Library
Show/Hide TOC

# 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.

## Definition

Namespace: Gemstone.IO
Assembly: Gemstone.Common (in Gemstone.Common.dll) Version: 1.0.164 -- Release Build+6bbd7ea5ff4ad8917f12d693358b77d083ee278e
C#
View Source Request Example

    [SecurityCriticalAttribute]
    public class SafeFileWatcher : IDisposable

VB
View Source Request Example

    <SecurityCriticalAttribute>
    Public Class SafeFileWatcher
    	Implements IDisposable

C++
View Source Request Example

    [SecurityCriticalAttribute]
    public ref class SafeFileWatcher : IDisposable

JavaScript
View Source Request Example

    Gemstone.IO.SafeFileWatcher = function();

    Type.createClass(
    	'Gemstone.IO.SafeFileWatcher',
    	null,
    	IDisposable);

Inheritance
  Object SafeFileWatcher

Implements
  IDisposable

## 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 thus 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.

## Constructors

SafeFileWatcher | Initializes a new instance of the SafeFileWatcher class.
SafeFileWatcher(String) | Initializes a new instance of the SafeFileWatcher class, given the specified directory to monitor.
SafeFileWatcher(String, String) | Initializes a new instance of the SafeFileWatcher class, given the specified directory and type of files to monitor.

## Properties

EnableRaisingEvents | Gets or sets a value indicating whether the component is enabled.
Filter | Gets or sets the filter string used to determine what files are monitored in a directory.
IncludeSubdirectories | Gets or sets a value indicating whether subdirectories within the specified path should be monitored.
InternalBufferSize | Gets or sets the size of the internal buffer.
NotifyFilter | Gets or sets the type of changes to watch for.
Path | Gets or sets the path of the directory to watch.
Site | Gets or sets an ISite for the SafeFileWatcher.
SynchronizingObject | Gets or sets the object used to marshal the event handler calls issued as a result of a directory change.

## Methods

BeginInit | Begins the initialization of a SafeFileWatcher used on a form or used by another component. The initialization occurs at run time.
Dispose | Releases all the resources used by the SafeFileWatcher object.
Dispose(Boolean) | Releases the unmanaged resources used by the SafeFileWatcher object and optionally releases the managed resources.
EndInit | Ends the initialization of a SafeFileWatcher used on a form or used by another component. The initialization occurs at run time.
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object)
Finalize | Terminates SafeFileWatcher instance making sure to release unmanaged resources. (Overrides ObjectFinalize)
GetHashCode | Serves as the default hash function. (Inherited from Object)
GetType | Gets the Type of the current instance. (Inherited from Object)
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object)
ToString | Returns a string that represents the current object. (Inherited from Object)
WaitForChanged(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.
WaitForChanged(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.

## Events

Changed | Occurs when a file or directory in the specified Path is changed.
Created | Occurs when a file or directory in the specified Path is created.
Deleted | Occurs when a file or directory in the specified Path is deleted.
Error | Occurs when the internal buffer overflows.
Renamed | Occurs when a file or directory in the specified Path is renamed.

## Extension Methods

GetEnumValueOrDefault | Gets the enumeration constant for value, if defined in the enumeration, or a default value. (Defined by EnumExtensions)
GetEnumValueOrDefaultT | Gets the enumeration constant for this value, if defined in the enumeration, or a default value. (Defined by EnumExtensions)

## See Also

#### Reference

Gemstone.IO Namespace