-1

Inside MainWindow.xaml.cs i tried this

fsw.SynchronizingObject = (System.ComponentModel.ISynchronizeInvoke)this;

but getting exception :

System.InvalidCastException: 'Unable to cast object of type 'WPF_Test.MainWindow' to type 'System.ComponentModel.ISynchronizeInvoke'.'

just using this in the end of the line

fsw.SynchronizingObject = this;

give the error

cannot convert from mainwindow to ISynchronizeInvoke

but then when trying to cast it's giving me the exception on the top.

Daniel Lip
  • 3,643
  • 6
  • 50
  • 98
  • As you noticed, the WPF Window class does not implement ISynchronizeInvoke. Use its Dispatcher in the FileSystemWatcher event handlers. – Clemens May 06 '22 at 11:38
  • `ISynchronizeInvoke` is a WinForms interface. Obviously FileSystemWatcher was introduced to .NET when WinForms was *the* standard UI Lib for .NET. Could be interesting to write a wrapper class, that delegates the 4 members of ISynchronizeInvoke, i.e. `Invoke`, `BeginInvoke`, `EndInvoke` and `InvokeRequired`, to the WPF Dispatcher members `Invoke`, `BeginInvoke`, `EndInvoke` and `CheckAccess`. Then you should be able to integrate WPF UI into FSW event callbacks seemlessly everywhere you have this setup. – lidqy May 06 '22 at 11:48

0 Answers0