1

Specific question. I've set up a program that watches for changes to a text file with numbers.

            FileSystemWatcher mikeWatcher = new FileSystemWatcher();
            System.IO.WaitForChangedResult postMike;
            string filePath = @"c:\path.txt";
            mikeWatcher.Path = Path.GetDirectoryName(filePath);
            mikeWatcher.Filter = Path.GetFileName(filePath);

            // Wait for change to the file
            postMike = mikeWatcher.WaitForChanged(System.IO.WatcherChangeTypes.Changed);

After this the code continues with the process once the WaitForChanged flag gets complete.

...

The issue I'm having is that while the flag is successfully triggered when I manually change the text file and hit save, when the other program (matlab) writes to and saves to that same file, the flag doesn't seem to be triggered.

I've confirmed that it is indeed the same file being changed (Path is correct) and that the change is registered in the "last modified date". Also, it does appear that the matlab process is closing the text file after saving so its not a release issue, I don't think.

Any thoughts or advice? It'd be easiest for me to change my c# code then the matlab code. All suggestions welcome!

5
  • 2
    Did you try subscribing to FWS's events instead of WaitForChanged by any chance? Commented Nov 4, 2014 at 21:09
  • As an extra point found during debugging. It looks like once the Matlab file touches the watched file, the C# program just freezes up. The two programs do not interface in any way except for this "hand-off" of the watched file. Even changing the "watched file" manually at that point doesn't yield the flag to be triggered. It only works if the text file is modified by changing it directlly in notepad. Thoughts? Commented Nov 4, 2014 at 21:12
  • @s.m. What's FWS events? Commented Nov 4, 2014 at 21:12
  • Uh, it's a typo, sorry. I meant FSW, for FileSystemWatcher. It exposes a Changed event that you can subscribe to. Keeping into account @HABJAN's answer, try subscribing to all of the events and see which one, if any, is firing. Commented Nov 4, 2014 at 21:14
  • Programs with a support phone number do not overwrite files. Commented Nov 4, 2014 at 21:42

3 Answers 3

2

The solution here is to stop using FileSystemWatcher, and instead use a timer to scan the directory for changes.

I have tried to use FileSystemWatcher in two projects and in both cases I found it too unreliable. It would miss some events, delay some events, and report some events twice. I gave up, and haven't used it since.

This answer may not be popular but I'm convinced that the only correct way to use FileSystemWatcher is in comments, like this:

// Tried FileSystemWatcher, but it was far too unreliable.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @NSFW. I think that's my conclusion as well. I just decided to avoid dealing with filesystem watcher.
2

I think the problem is that it's possible that matlab deletes the file and creates new one with exactly a same name so the file ends up not being changed but newly created. You should trap Deleted and Created events also, not changed only.

Try the code from this example: http://msdn.microsoft.com/en-us/library/t6xf43e0(v=vs.110).aspx

2 Comments

Will try this.. but I don't think the matlab file is being deleted. The contents are being cleared and the file is being rewritten to, but I will give this a shot. Thank for the suggestion @Habjan
You can get similar non-determinism just using Notepad.exe to trigger different types of changes. Or even just "echo foo > sometime.txt" at the command line. FileSystemWatcher is simply unpredictable.
0

I had the exact same problem and solved it by setting FW.EnableRaisingEvents = true;.

Strange because I don't use the events but it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.