private void StartWatcher(ShellObject shellObject) { _watcher?.Dispose(); eventStack.Children.Clear(); txtPath.Text = shellObject.ParsingName; _watcher = new ShellObjectWatcher(shellObject, chkRecursive.IsChecked ?? true); _watcher.AllEvents += AllEventsHandler; _watcher.Start(); }
private void TestMethod(string eventName, Action <string> test, string path) { var shellObject = ShellObject.FromParsingName(path); using (var evt = new AutoResetEvent(false)) using (var watcher = new ShellObjectWatcher(shellObject, true)) { bool success = false; var successEvent = new Action(() => { success = true; _ = evt.Set(); }); var changedHandler = new EventHandler <ShellObjectChangedEventArgs>((sender, args) => successEvent()); var renamedHandler = new EventHandler <ShellObjectRenamedEventArgs>((sender, args) => successEvent()); //register for event System.Reflection.MethodInfo addMethod = typeof(ShellObjectWatcher) .GetEvents() .FirstOrDefault(x => x.Name.Equals(eventName, StringComparison.InvariantCultureIgnoreCase)) .GetAddMethod(); Type paramType = addMethod.GetParameters()[0].ParameterType .GetGenericArguments()[0]; if (paramType == typeof(ShellObjectChangedEventArgs)) { addMethod.Invoke(watcher, new object[] { changedHandler }); } else if (paramType == typeof(ShellObjectRenamedEventArgs)) { addMethod.Invoke(watcher, new object[] { renamedHandler }); } else { throw new Exception("Unknown handler type."); } //start watcher.Start(); test(path); _ = evt.WaitOne(TimeoutMS); Assert.True(success); } }