FileSystemWatcher fires events on a different thread than the thread that it was registered on (mainly the UI thread). If you attempt to update a UI element (such as the FileView's treeview) you will get a "cross-thread..." exception as I showed in class. To avoid this problem, simply Set the "SynchronizingObject" property of the FileSystemWatcher to the FileView (i.e. a UI object in the Main thread) , this will force the FileSystemWatcher event callback handler to be called on the Main UI thread.
FileSystemWatcher fw = new FileSystemWatcher(path, "*.wav");
fw.EnableRaisingEvents = true;
fw.NotifyFilter = NotifyFilters.FileName;
fw.SynchronizingObject = this;
FileSystemWatcher fw = new FileSystemWatcher(path, "*.wav");
fw.EnableRaisingEvents = true;
fw.NotifyFilter = NotifyFilters.FileName;
fw.SynchronizingObject = this;
I did as you told in class:
ReplyDeleteInvoke(new MethodInvoker(() => RenewFolder(folder)));
seems works Ok too with InvokeRequired :)
I finished with FileSystemWatcher (halliluia!!! :)))) and probably will do config at the end because it looks yaki for me heh...
Gr8. This is the general mechanism for synchronizing any call (coming in from a non-main UI thread) back to the UI thread. I was not sure if people understood this delegate syntax.
ReplyDeleteI was happy to learn how to implement the lambda approach to delegates... i bought a couple c# books at the beginning of the semester and didn't understand what they were talking about. But now I do. Also, this was a question on Stackoverflow that I could answer. My rep is up to 120 now. haha.
ReplyDelete