public static void FileWatchingThread(object objFileSystemObjectTaskWatcher) { FileSystemObjectTaskWatcher fileSystemObjectTaskWatcher = (FileSystemObjectTaskWatcher)objFileSystemObjectTaskWatcher; FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(fileSystemObjectTaskWatcher.Path); fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; fileSystemWatcher.IncludeSubdirectories = fileSystemObjectTaskWatcher.IncludeSubDirectories; fileSystemWatcher.Filter = "*"; fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemObjectTaskWatcher.OnFileSystemObjectChanged); fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemObjectTaskWatcher.OnFileSystemObjectChanged); fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemObjectTaskWatcher.OnFileSystemObjectChanged); fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemObjectTaskWatcher.OnFileSystemObjectRenamed); fileSystemWatcher.EnableRaisingEvents = true; Console.WriteLine("{0} : {1} : FileWatchingThread running in \"{2}\" on \"{3}\" with subdirectories {4} and resolution {5} ms", DateTime.Now, Thread.CurrentThread.ManagedThreadId, Environment.CurrentDirectory, fileSystemWatcher.Path, (fileSystemWatcher.IncludeSubdirectories?"included":"not included"), fileSystemObjectTaskWatcher._eventResolutionMilliseconds); fileSystemObjectTaskWatcher._exitEvent.WaitOne(); Console.WriteLine("{0} : {1} : FileWatchingThread exiting", DateTime.Now, Thread.CurrentThread.ManagedThreadId); }
private static void ProcessingThread(object objFileSystemObjectTaskWatcher) { FileSystemObjectTaskWatcher fileSystemObjectTaskWatcher = (FileSystemObjectTaskWatcher)objFileSystemObjectTaskWatcher; bool fileSystemObjectEventDetected = false; bool exitEvent = false; while ((!exitEvent) && (fileSystemObjectTaskWatcher.Running)) { WaitHandle[] waitHandles = new WaitHandle[] { fileSystemObjectTaskWatcher._fileSystemObjectWatchEvent, fileSystemObjectTaskWatcher._exitEvent }; int waitIndex = WaitHandle.WaitAny(waitHandles, fileSystemObjectTaskWatcher._eventResolutionMilliseconds); if (waitIndex != System.Threading.WaitHandle.WaitTimeout) { // Not a timeout switch (waitIndex) { case 0: /* FileSystemObjectTaskWatcher._fileSystemObjectWatchEvent */ // File System Object Watcher Event occurred Console.WriteLine("{0} : {1} : Detected a File System Object Event", DateTime.Now, Thread.CurrentThread.ManagedThreadId); fileSystemObjectEventDetected = true; break; case 1: /* FileSystemObjectTaskWatcher._exitEvent */ Console.WriteLine("{0} : {1} : Detected an Exit Event", DateTime.Now, Thread.CurrentThread.ManagedThreadId); exitEvent = true; break; default: Console.WriteLine("{0} : {1} : Detected an Unknown Event with WaitIndex {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, waitIndex); break; } } // Not a timeout else { // Timeout occurred Console.WriteLine("{0} : {1} : Timeout after {2} ms waiting for File System Object Event", DateTime.Now, Thread.CurrentThread.ManagedThreadId, fileSystemObjectTaskWatcher._eventResolutionMilliseconds); if (fileSystemObjectEventDetected) { // ProcessingThread this File System Object Event fileSystemObjectEventDetected = false; if (fileSystemObjectTaskWatcher._taskNameToExecute != null) { Console.WriteLine("{0} : {1} : Running task \"{2}\"", DateTime.Now, Thread.CurrentThread.ManagedThreadId, fileSystemObjectTaskWatcher._taskNameToExecute); fileSystemObjectTaskWatcher.ExecuteTask(); } } // ProcessingThread this File System Object Event } // Timeout occurred } // while Console.WriteLine("{0} : {1} : ProcessingThread exiting", DateTime.Now, Thread.CurrentThread.ManagedThreadId); }
static void Main(string[] args) { if (args.Count() > 0) { string directoryToWatch = args[0]; bool includeSubDirectories = false; int eventResolutionMilliseconds = 10000; string taskNameToExecute = null; if (args.Count() > 1) { includeSubDirectories = (args[1].ToLower() == "/s" ? true : false); } if (args.Count() > 2) { try { eventResolutionMilliseconds = System.Convert.ToInt32(args[2]); } catch (Exception) { } Console.WriteLine("Event Resolution is {0} milliseconds", eventResolutionMilliseconds); } if (args.Count() > 3) { taskNameToExecute = args[3]; Console.WriteLine("Task to execute is \"{0}\"", taskNameToExecute); } try { using (FileSystemObjectTaskWatcher fileSystemObjectTaskWatcher = new FileSystemObjectTaskWatcher( directoryToWatch, includeSubDirectories, eventResolutionMilliseconds, taskNameToExecute)) { // Wait for the user to quit the program fileSystemObjectTaskWatcher.BeginProcessing(); Console.WriteLine("Press \'q\' to quit File Watching"); int response = 0; do { response = Console.Read(); } while (response != 'q'); } } catch (Exception eek) { Console.WriteLine("Exception: \"{0}\"", eek.ToString()); } } }