Notes on events

The following are some notes on gotcha points with filesystem events that you should be aware of:

  • Common filesystem operations may raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional filesystem events that are detected by the watcher.
  • The watcher can watch disks if they are not switched or removed. The watcher does not raise events for CDs and DVDs because time stamps and properties cannot change. Remote computers must have one of the required platforms installed for the component to function properly.
  • If multiple watcher objects are watching the same UNC path in Microsoft Windows XP prior to Service Pack 1 (SP1), or Windows 2000 SP2 or earlier, then only one of the objects will raise an event. On machines running Microsoft Windows XP SP1 and newer, Windows 2000 SP3 or newer, or Windows Server 2003, all watchers will raise the appropriate events.

With that out of the way, we need to now write our main program.cs file. It will be the same as all our others, but with some very special additions:

private static void Main(string[] args)
{
HostFactory.Run(config =>
{
config.Service<Program>(s =>
{
s.ConstructUsing(() => new Program());
s.BeforeStartingService((hostStart) =>
{
CreateTopology("EvolvedAI", "FileSystemChanges");
if (!Directory.Exists(_testDir))
Directory.CreateDirectory(_testDir);
});
s.WhenStarted((service, host) => true);
s.WhenStopped((service, host) => true);
s.WhenFileSystemCreated(ConfigureDirectoryWorkCreated, FileSystemCreated);
s.WhenFileSystemChanged(ConfigureDirectoryWorkChanged, FileSystemCreated);
s.WhenFileSystemRenamed(ConfigureDirectoryWorkRenamedFile, FileSystemRenamedFile);
s.WhenFileSystemRenamed(ConfigureDirectoryWorkRenamedDirectory, FileSystemRenamedDirectory);
s.WhenFileSystemDeleted(ConfigureDirectoryWorkDeleted, FileSystemCreated);
});
});
Console.ReadKey();
}

As you can see with the preceding highlighted code, we have highlighted the sections on this main module that are specific to FileSystemWatcher. You will notice that there are event handlers for files created, changed, renamed, deleted, and directories renamed.

Now let's look at the handlers themselves. There is one for each event we handle. The main point to notice is that when an event occurs, we gather all the information and publish a message for anyone who is listening:

private static void FileSystemCreated(
TopshelfFileSystemEventArgs
topshelfFileSystemEventArgs)
{
//Here we create our change message
FileSystemChangeMessage m = new FileSystemChangeMessage
{

Note that we are not using DateTime.Now here, but the NodaTime instant. This ensures consistent time throughout the entire ecosystem when events are reported and/or logged:

ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)topshelfFileSystemEventArgs.ChangeType,
EventType = (int)topshelfFileSystemEventArgs.FileSystemEventType,
FullPath = topshelfFileSystemEventArgs.FullPath,
OldPath = topshelfFileSystemEventArgs.OldFullPath,
Name = topshelfFileSystemEventArgs.Name,
OldName = topshelfFileSystemEventArgs.OldName
};

Here we publish our message:

PublishMessage(m, "EvolvedAI", "");
Console.WriteLine("*********************");
Console.WriteLine("ChangeType = {0}", topshelfFileSystemEventArgs.ChangeType);
Console.WriteLine("FullPath = {0}", topshelfFileSystemEventArgs.FullPath);
Console.WriteLine("Name = {0}", topshelfFileSystemEventArgs.Name);
Console.WriteLine("FileSystemEventType {0}", topshelfFileSystemEventArgs.FileSystemEventType);
Console.WriteLine("*********************");
}
private static void FileSystemRenamedFile(
TopshelfFileSystemEventArgs
topshelfFileSystemEventArgs)
{
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)topshelfFileSystemEventArgs.ChangeType,
EventType = (int)topshelfFileSystemEventArgs.FileSystemEventType,
FullPath = topshelfFileSystemEventArgs.FullPath,
OldPath = topshelfFileSystemEventArgs.OldFullPath,
Name = topshelfFileSystemEventArgs.Name,
OldName = topshelfFileSystemEventArgs.OldName
};
PublishMessage(m, "EvolvedAI", "");
Console.WriteLine("*********************");
Console.WriteLine("Rename File");
Console.WriteLine("ChangeType = {0}", topshelfFileSystemEventArgs.ChangeType);
Console.WriteLine("FullPath = {0}", topshelfFileSystemEventArgs.FullPath);
Console.WriteLine("Name = {0}", topshelfFileSystemEventArgs.Name);
Console.WriteLine("FileSystemEventType {0}", topshelfFileSystemEventArgs.FileSystemEventType);
Console.WriteLine("*********************");
}
private static void FileSystemRenamedDirectory(
TopshelfFileSystemEventArgs
topshelfFileSystemEventArgs)
{
FileSystemChangeMessage m = new FileSystemChangeMessage
{
ChangeDate = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
ChangeType = (int)topshelfFileSystemEventArgs.ChangeType,
EventType = (int)topshelfFileSystemEventArgs.FileSystemEventType,
FullPath = topshelfFileSystemEventArgs.FullPath,
OldPath = topshelfFileSystemEventArgs.OldFullPath,
Name = topshelfFileSystemEventArgs.Name,
OldName = topshelfFileSystemEventArgs.OldName
};
PublishMessage(m, "EvolvedAI", "");
Console.WriteLine("*********************");
Console.WriteLine("Rename Dir");
Console.WriteLine("ChangeType = {0}", topshelfFileSystemEventArgs.ChangeType);
Console.WriteLine("FullPath = {0}", topshelfFileSystemEventArgs.FullPath);
Console.WriteLine("Name = {0}", topshelfFileSystemEventArgs.Name);
Console.WriteLine("FileSystemEventType {0}", topshelfFileSystemEventArgs.FileSystemEventType);
Console.WriteLine("*********************");
}

This is what our application looks like when it is running, and we drop files into our test directory:

Here is another screenshot that shows memory profiling messages being processed:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset