Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

After reading a number of articles (directed to by the good guys here :-)), i was able to come across FileSystemWatcher class that enabled me to monitor changes on various locations in my computer. To begin with, i tried on Windows Forms and it all worked all fine. Given that FileSystemWatcher, by default, runs on a separate thread from the Main Thread, the folks at Microsoft made this line:

watcher.SynchronizingObject = this;


which allows the FileSystemWatcher to work alongside the Main Thread, thus solving the illegal cross thread error.


Tranfering the logic in WPF, the same line is actually a compile error saying:

C#
Cannot implicitly convert type 'MyProject.MainWindow' to 'System.ComponentModel.ISynchronizeInvoke'. An explicit conversion exists (are you missing a cast?)


Why could this be the case and what could be the solution?

Thanks :-)
Posted

1 solution

I would suggest that you don't try to tie the FileSystemWatcher to the UI directly. Instead when you need to update the UI based on the events of the FileSystemWatcher you can use Dispatcher.CheckAccess[^] method to resolve if the thread is correct to update the UI.
 
Share this answer
 
Comments
Wamuti 9-Sep-12 11:56am    
Thanks. I tried it and it works. Thanks!!
Wendelius 9-Sep-12 12:02pm    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900