private void fsWather_Deleted(object sender, FileSystemEventArgs e) { lock (hstbWather) { hstbWather.Add(e.FullPath, e); } WatcherProcess watcherProcess = new WatcherProcess(sender, e); watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted); watcherProcess.OnDeleted += new FileSystemEventHandler(WatcherProcess_OnDeleted); Thread tdDeal = new Thread(watcherProcess.Process); tdDeal.Start(); }
/// <summary> /// 停止监控 /// </summary> /* public void Stop() { fsWather.EnableRaisingEvents = false; }*/ /// <summary> /// filesystemWatcher 本身的事件通知处理过程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void fsWather_Renamed(object sender, RenamedEventArgs e) { lock (hstbWather) //To ensure that when a thread located in the critical section of code , another thread enters the critical section { hstbWather.Add(e.FullPath, e); //Adds an element with the specified key and value into the Hashtable. } WatcherProcess watcherProcess = new WatcherProcess(sender, e); watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);//Remove the corresponding change file Hashtable key , or the next change of this file can not be triggered your business logic . watcherProcess.OnRenamed += new RenamedEventHandler(WatcherProcess_OnRenamed); Thread thread = new Thread(watcherProcess.Process); //Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started. thread.Start(); //Causes a thread to wait the number of times defined by the iterations parameter. }
private void fsWather_Changed(object sender, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed) { if (hstbWather.ContainsKey(e.FullPath)) { WatcherChangeTypes oldType = ((FileSystemEventArgs)hstbWather[e.FullPath]).ChangeType; if (oldType == WatcherChangeTypes.Created || oldType == WatcherChangeTypes.Changed) { return; } } } lock (hstbWather) { hstbWather.Add(e.FullPath, e); } WatcherProcess watcherProcess = new WatcherProcess(sender, e); watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted); watcherProcess.OnChanged += new FileSystemEventHandler(WatcherProcess_OnChanged); Thread thread = new Thread(watcherProcess.Process); thread.Start(); }