In a WPF app, is there a object I can assign to FileSystemWatcher.SynchronizingObject?
I can make my own, but if there is one available, I would like to use it.
In a WPF app, is there a object I can assign to FileSystemWatcher.SynchronizingObject?
I can make my own, but if there is one available, I would like to use it.
Reflector shows that the only class that implements ISynchronizeInvoke
(i.e., the type of the FileSystemWatcher.SynchronizingObject
property) is System.Windows.Form.Control
(and its subclasses); there do not appear to be any WPF objects that implement this interface.
there is one way. FileSystemWatcher when you enabling events (EnableRaisingEvents = true) creates it's own thread to monitor the FS events. via ISynchronizeInvoke it can async invoke members of your Form for example, (it's thread can async interact with the main thread - UI thread). in WPF there's no implementation of ISynchronizeInvoke, but there is a possibility to interact with the UI thread, via Dispatched property of your Window like this:
var fsw = new FileSystemWatcher() { (setting the properties: Path, Filter, NotifyFilter, etc.) };
fsw.Created += (sender, e) => { Dispatcher.Invoke(new Action((params_identifiers) => { here the code wich interacts with your IU elements }), here_params); };
... in this way (via Dispatcher.Invoke) with the rest of events
fsw.EnableRaisingEvents = true;
You need to create an ISynchronizeInvoke object that wraps the System.Windows.Threading.Dispatcher instance from the Window. That class is the closest thing WPF has to an ISynchronizeInvoke object.
Inside the wrapper class, simply forward the BeginInvoke call to the dispatcher you've wrapped. I did a bit extra work and also wrapped the DispatcherOperation that results from the Dispatcher.BeginInvoke method to call its Wait method inside the ISynchronizeInvoke.EndInvoke method.
Everything seems to be working correctly so far, it's too bad Microsoft didn't see fit to have the Dispatcher class implement the interface for ease of use.