web analytics

Home of my tech rants, free programs, and a story or two…

Wow Microsoft… just wow.

So I’m sitting here writing some code in .NET and I have myself a File System Watcher in my code to check if a file has been changed and if it has, it’s supposed to load the file and process any changes. In this case, it’s a log file and if the log file changes anywhere else in the program the log viewing window should reload it. Seems right? Yes.

OK, so now onto the bug. Apparently there’s a bug in which the File System Watcher will fire off multiple file changed events one after another even though there was only one change to one file. Yes, that’s right. Multiple events.

So here’s the code…

private System.DateTime dateLastFileSystemWatcherEventRaised;

private void logFileWatcher_Changed(object sender, IO.FileSystemEventArgs e)
{
    // This hack is required because of a bug in the File System Watcher that causes it to fire multiple events one after
    // another even though there was only one change to the file we are watching. ARG Microsoft! You stupid idiots!
    if 1 {
        return; // Crap, multiple events have been fired... we need to exit this routine.
    }
    dateLastFileSystemWatcherEventRaised = System.DateTime.Now;
  
    // Put your event code here.
}

Of you want a VB.NET example…

Private dateLastFileSystemWatcherEventRaised As Date

Private Sub logFileWatcher_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles logFileWatcher.Changed
	' This hack is required because of a bug in the File System Watcher that causes it to fire multiple events one after
	' another even though there was only one change to the file we are watching. ARG Microsoft! You stupid idiots!
	If (Date.Now.Subtract(dateLastFileSystemWatcherEventRaised).TotalMilliseconds < 500) Then
		Exit Sub ' Crap, multiple events have been fired... we need to exit this routine.
	End If
	dateLastFileSystemWatcherEventRaised = Date.Now
	
	' Put your event code here.
End Sub
  1. System.DateTime.Now.Subtract(dateLastFileSystemWatcherEventRaised).TotalMilliseconds < 500[]