public FileWatch(Form form, string path, string filter) { Path = path; Filter = filter; _form = form; _watcher = new FileSystemWatcher { Path = path, Filter = filter }; _watcher.Changed += (o, args) => _form.Invoke((MethodInvoker) delegate { _lastSeen[args.FullPath] = DateTime.Now; if (_timer == null) { _timer = new Timer { Interval = 500 }; _timer.Tick += (sender, eventArgs) => { var now = DateTime.Now; var toRemove = new List <string>(); foreach (var kv in _lastSeen) { // check if it's been over 1000 ms since the last event if (now - kv.Value > TimeSpan.FromMilliseconds(1000)) { // notify all our listeners var cds = new WatcherStructs.COPYDATASTRUCT { cbData = 0, lpData = IntPtr.Zero }; foreach (var x in _listeners) { cds.dwData = (IntPtr)x.token; int res = SendMessage(x.hwnd, WatcherStructs.WM_COPYDATA, (int)_form.Handle, ref cds); } toRemove.Add(kv.Key); } } // remove the tracking on all the files that have been processed toRemove.ForEach(x => _lastSeen.Remove(x)); if (_lastSeen.Count == 0) { _timer.Stop(); } }; _timer.Start(); } if (!_timer.Enabled) { _timer.Start(); } }); }
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref WatcherStructs.COPYDATASTRUCT lParam);