You repeat the start code in NotAccessibleError. I believe that a simple rewrite of start would improve your code. The following is an example. Here m_Watcher has been replaced by file_watcher. Also, file_watcher is expected to be initialized to null in the class variable declarations.
public static void start ( )
{
if ( file_watcher != null )
{
file_watcher.Created -= OnCreated;
file_watcher.Error -= OnError;
file_watcher.Dispose ( );
file_watcher = null;
}
file_watcher = new FileSystemWatcher ( )
{
EnableRaisingEvents = true,
Filter = Filter,
IncludeSubdirectories = false,
NotifyFilter = NotifyFilters.FileName,
Path = Path
};
file_watcher.Created +=
new FileSystemEventHandler ( OnCreated );
file_watcher.Error +=
new ErrorEventHandler ( OnError );
}
You may be having problems by not deleting event handlers before adding new ones. In the revised start, that is repaired by removing the event handlers if file_watcher is not null.
By implementing this revision, all that needs be done is to invoke start ( ) appropriately in NotAccessibleError.
I changed watcher to FileWatcher. For the revised start to work, the FileWatcher constructor should not instantiate a new FileSystemWatcher. The following code is the revised class constants, class variables, and class constructor.
public class FileWatcher
{
const int MAXIMUM_ATTEMPTS = 120;
const int TIMEOUT = 5000;
static FileSystemWatcher file_watcher = null;
static string filter = String.Empty;
static string path = String.Empty;
public FileWatcher ( string _path,
string _filter )
{
path = _path;
filter = _filter;
}
Hope that helps.