[Mono-dev] FileSystemWatcher Class

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Thu Mar 20 19:59:39 UTC 2014


Can anybody comment on the state of FileSystemWatcher class, particularly on OSX?  It looks good on the class compatibility page, and in fact I've been using it flawlessly for months on windows, and also flawlessly on linux for weeks...  But now I'm trying to use it on OSX and I'm running into all sorts of problems.   Using MDK 3.2.7 on fully updated mavericks.

To test, I build the sample code below, and then open a terminal window, and start doing simple commands such as these.  As you can see, things immediately go wrong.  In some cases, events seem to be missing.  In other cases, events seem to be stuck in a queue or buffer somewhere, until some other event occurs which is able to shake the offending events loose.  

touch a b c d e
Expected:
    Created '/Users/eharvey/fswatcher/a'
    Changed '/Users/eharvey/fswatcher/a'
    Created '/Users/eharvey/fswatcher/b'
    Changed '/Users/eharvey/fswatcher/b'
    Created '/Users/eharvey/fswatcher/c'
    Changed '/Users/eharvey/fswatcher/c'
    Created '/Users/eharvey/fswatcher/d'
    Changed '/Users/eharvey/fswatcher/d'
    Created '/Users/eharvey/fswatcher/e'
    Changed '/Users/eharvey/fswatcher/e'
Actual:
    Created '/Users/eharvey/fswatcher/a'
    Created '/Users/eharvey/fswatcher/b'
    Created '/Users/eharvey/fswatcher/c'
    Created '/Users/eharvey/fswatcher/d'
    Created '/Users/eharvey/fswatcher/e'

rm a b c d e
Expected:
    Deleted '/Users/eharvey/fswatcher/a'
    Deleted '/Users/eharvey/fswatcher/b'
    Deleted '/Users/eharvey/fswatcher/c'
    Deleted '/Users/eharvey/fswatcher/d'
    Deleted '/Users/eharvey/fswatcher/e'
Actual:
    Deleted '/Users/eharvey/fswatcher/a'

touch foo
Expected:
    Created '/Users/eharvey/fswatcher/foo'
    Changed '/Users/eharvey/fswatcher/foo'
Actual:
    Deleted '/Users/eharvey/fswatcher/b'
    Created '/Users/eharvey/fswatcher/foo'

rm foo
Expected:
    Deleted '/Users/eharvey/fswatcher/foo'
Actual:
    Deleted '/Users/eharvey/fswatcher/c'

touch bar
Expected:
    Created '/Users/eharvey/fswatcher/bar'
    Changed '/Users/eharvey/fswatcher/bar'
Actual:
    Deleted '/Users/eharvey/fswatcher/d'
    Created '/Users/eharvey/fswatcher/bar'

rm bar
Expected:
    Deleted '/Users/eharvey/fswatcher/bar'
Actual:
    Deleted '/Users/eharvey/fswatcher/e'

Sample code:


using System;
using System.IO;
using System.Threading;

namespace FunWithFileSystemWatcher
{
	class MainClass
	{
		public static void Main (string[] args)
		{

			var fswatcher = new FileSystemWatcher();
			fswatcher.Path = "/Users/eharvey/fswatcher";
			fswatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | 
				NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
			fswatcher.IncludeSubdirectories = true;
			fswatcher.InternalBufferSize = 65536;      // documentation says, must not be larger than 64k
			fswatcher.Renamed += (object sender, RenamedEventArgs e) => {
				Console.Error.WriteLine ("Renamed '" + e.OldFullPath + "' to '" + e.FullPath + "'");
				Console.Error.Flush ();
			};
			fswatcher.Deleted += (object sender, FileSystemEventArgs e) => {
				Console.Error.WriteLine("Deleted '"+e.FullPath+"'");
				Console.Error.Flush ();
			};
			fswatcher.Changed += (object sender, FileSystemEventArgs e) => {
				Console.Error.WriteLine("Changed '"+e.FullPath+"'");
				Console.Error.Flush ();
			};
			fswatcher.Created += (object sender, FileSystemEventArgs e) => {
				Console.Error.WriteLine("Created '"+e.FullPath+"'");
				Console.Error.Flush ();
			};
			fswatcher.Error += (object sender, ErrorEventArgs e) => {
				Console.Error.WriteLine ("Error");
				Console.Error.Flush ();
				throw new Exception ("Error");
			};

			fswatcher.EnableRaisingEvents = true;

			Thread.Sleep (int.MaxValue);
		}
	}
}


More information about the Mono-devel-list mailing list