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

WCF Dos and Don'ts

Do not inherit interfaces from other interfaces. Interfaces are contracts. Contracts must be explicit, otherwise the flexibility and maintainability of your design would run into issues. An interface is a pure contract definition and should remain explicit and pure. Consider the following example: [ServiceContract] public interface IMyService1 { [OperationContract] void Test1(); } [ServiceContract] public interface IMyService2 : IMyService1 { [OperationContract] void Test2(); } [ServiceBehavior(…)] class MyService : IMyService2, IMyService1 { #region IMyService2 Members public void Test2() { } #endregion #region IMyService1 Members public void Test1() { } #endregion } What happens when you Publish your Service and open its communication channels. If you do the following: ServiceHost serviceHost = new ServiceHost(typeof(MyService, …) ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService1), typeof(IMyService2)); You will get

How to turn off the annoying adobe updater

Mainly for peole who only installed adobe reader, this becomes an annoying intrusive behavior. Who cares about the updated version of Adobe. 1- Run Adobe Reader 2- Invoke the "Edit" > "Preferences" menu 3- Select the Updater node at the bottom. 4- Select the "Do not download or install updates automatically" 5- Press OK. Done!!!!!!!!!!!! Phew

TF31001 error when connecting to TFS 2010

The Server returned the following error: 246021. An error occurred while processing your request. Technical Information (for Administrator): SQL Server Error 1934 (retry) This error is caused due to SQL Server Server settings of "noCount" and/or "numeric round about" being turned on. Run ssms.exe, right click on the SQl Server node, and invoke the Proerties dialog. Select the "Connections" node, and turn off both items.