Filewatcher API is a dotnet API to trigger action based on file-change events.

Workaround for multiple file change events

FileWatcher API has a bug which causes it to send multiple file change events. This happens because FileSystemWatcher monitors the operating system activities, all events that applications like Notepad fire will be picked up.

A easy workaround for this issue:

let mutable lastChangedEvent = System.DateTime.Now
watcher.Changed.Add(fun args ->
    // if time of last event fired is more than 500ms,
    // then perform action and update lastChangedEvent 
    // else ignore
    if (System.DateTime.Now.Subtract lastChangedEvent).TotalMilliseconds > 500
        printfn "file changed"
        lastChangedEvent <- System.DateTime.Now