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

Wave Undo

To implement Undo, the simplest way is to use a MemoryStream object in your WaveDisplayForm (MDI Child) and simply serialize the Wave (which should be marked as [Serializable] to the stream and hold on to the stream object. private MemoryStream _undoStream = new MemoryStream(); private bool _canUndo = false; public void SaveForUndo() {      BinaryFormatter bf = new BinaryFormatter();      bf.Serialize(_undoStream, wave);      _undoStream.Flush();      // This rewinds the stream so that when it is used for deserialization, it's ready to use,     // otherwise, the deserialization will start deserializing from the end of the stream and would fail.      _undoStream.Position = 0;      _canUndo = true; }   public void Undo() {      BinaryFormatter bf = new BinaryFormatter();      ...

Save All/Format All Documents in Visual Studio

Sub FormatAll()    For Each proj As Project In DTE.Solution.Projects      FormatFileRecur(proj.ProjectItems())    Next End Sub Sub FormatFileRecur(ByVal projectItems As EnvDTE.ProjectItems)    For Each pi As EnvDTE.ProjectItem In projectItems      If pi.Collection Is projectItems Then        Dim pi2 As EnvDTE.ProjectItems = pi.ProjectItems        Try          If pi.Name.EndsWith(".cs") Then            If Not (pi.Name.EndsWith("Designer.cs")) Then               If Not pi.IsOpen Then pi.Open(Constants.vsViewKindCode)             pi.Document.Activate()             DTE.ExecuteCommand("Edit.FormatDocum...