Skip to main content

FileSystemWatcher

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;

Comments

  1. I did as you told in class:
    Invoke(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...

    ReplyDelete
  2. 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.

    ReplyDelete
  3. I 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

Post a Comment

Popular posts from this blog

Track Files in Visual Studio

By default, Visual Studio’s Solution Explorer will update its selected item based on the currently active document. This is extremely annoying as it keeps expanding your projects and folders until your solution explorer becomes unmanageable and unusable, unless you collapse all Projects (Only available if you have Visual Studio Power Commands) installed) and even then, after collapsing the entire solution, the saga returns back. Turn off Active tracking 1. Turn your Active tracking off by going to Tools > Options 2. Select “Projects and Solutions” node 3. Uncheck the “Tack Active Item in Solution Explorer” Add a macro to do on-demand tracking 4. Tools > Marcos > Macro Explorer 5. You will see “MyMacros” Module (Node). Right mouse click on MyMacros and invoke “New Module…” 6. Name it anything you like. I Named it Utilities 7. Right mouse click on Utilizes and invoke “new Macro…” menu. The Macros IDE window opens. 8. Paste in the following into it within the Publ...