An easy way to reproduce this is with Visual Studio. When you save a file in Visual Studio, it actually creates a temporary file and then replaces the main file with the temporary file. There are several steps in that process. If you watch a directory and save a file with Visual Studio, only some of the steps will be reported and it will be inconsistent which will be reported. For example, here's what was reported the first time I saved a particular file:
CREATE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\1pufrzgv.y11~
And here's what was reported the second time:
CREATE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\0oo5h2cc.aff~
CREATE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtml~RF863f7e1.TMP
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home
I took a look at the code and this is caused by using code like this:
while (true)
{
var e = w.WaitForChanged(changes);
/// Process...
}
WaitForChanged
synchronously waits for changes in the directory of the type specified in changes
. If changes in the directory happen when processing a change notification, those changes are are not queued up anywhere and are lost.
The fix is to instead use the Created
, Changed
, Deleted
, and Renamed
events of the FileSystemWatcher
. The delegate specified will be called asynchrously when a change in the directory occurs.
I'll be sending a pull request shortly that makes this change. Saving that same file in Visual Studio with this code results in the following being reported, much more than got reported before:
CREATE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\ayjxpflb.rqc~
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home
MODIFY C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\ayjxpflb.rqc~
CREATE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtmlRF864a436.TMP
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home
DELETE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtml
MODIFY C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtmlRF864a436.TMP
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home
MOVED_FROM C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\ayjxpflb.rqc~
MOVED_TO C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtml
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home
DELETE C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home\Home.cshtml~RF864a436.TMP
MODIFY,ISDIR C:\Users\Greg\Documents\Programming\animerecs-git\AnimeRecs.Web\Modules\Home