create a new background worker to do our task while the main thread stays awake. If ( fileModifiedTable != File.GetLastWriteTime( e.FullPath ).ToString() ) If ( fileModifiedTable.TryGetValue( e.FullPath, out sEmpty ) ) check if we already have this value in our dictionary. Private void fswFileWatch_Changed( object sender, FileSystemEventArgs e ) Private static IDictionary fileModifiedTable = new Dictionary() // used to keep track of our changed events Using System.Diagnostics // used for file information using System.Threading // used for backgroundworker It then sets the value after starting the new thread. It compares that value, and if it is the same, it suppresses the events. It uses a dictionary to track the date and time of the last write of the file. I would like to thank everyone in this thread as I have used it in many references when battling this issue. Sorry for the grave dig, but I've been battling this issue for a while now and finally came up with a way to handle these multiple fired events. It has the downside of having a Dictionary growing in memory as files are monitored and eating more and more memory, but I've read somewhere that the amount of files monitored depends on the FSW's internal buffer, so maybe not that critical.ĭunno how MD5 computing time would affect your code's performances either, careful =\ It will easily avoid the twice fired event even with a smaller timer interval but in my case 1000 was alright since I was happier with missing few changes than with flooding the mailbox with > 1 message per second.Īt least it works just fine in case several files are changed at the exact same time.Īnother solution I've thought of would be to replace the list with a dictionary mapping files to their respective MD5, so you wouldn't have to choose an arbitrary interval since you wouldn't have to delete the entry but update its value, and cancel your stuff if it hasn't changed. This is the solution I used to solve this issue on a project where I was sending the file as attachment in a mail. Timer.Elapsed = (timerElapsedSender, timerElapsedArgs) => Private void OnChanged(object source, FileSystemEventArgs e) Here's my approach : // Consider having a List named _changedFiles Then if the hashtable has the filepath that is modified and it's time value is the same as the currently notified file's change then I know it is the duplicate of the event and ignore it. Mainly I create a hashtable to store file write time information. lastly we update the last write time in the hashtableįileWriteTime = currentLastWriteTime Log( "A CHANGE has occured with " path ) or stored path has different time of write then the one now is inspectedįileWriteTime.ToString() != currentLastWriteTime String currentLastWriteTime = File.GetLastWriteTime( e.FullPath ).ToString() Private void fsw_sync_Changed(object source, FileSystemEventArgs e) This is how I do eliminate the duplicate change event when I write to a file ( which flags the folder containing the file to be modified as well ) : private Hashtable fileWriteTime = new Hashtable() When I change something in a folder on the host I want all the changes to be uploaded, synced onto the virtual server via Ftp. I am developing files on the Windows host. My scenario is that I have a virtual machine with a Linux server in it. In some applications you might be able to get around this by using the NotifyFilter property, but my experience is says that sometimes you have to do some manual duplicate filtering (hacks) as well.Ī while ago I bookedmarked a page with a few FileSystemWatcher tips. Now this bit of text is about the Created event, but the same thing applies to other file events as well. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up. Other applications may perform in the same manner. Notepad writes to the disk in batches that create the content of the file and then the file attributes. This is because Notepad performs multiple file system actions during the writing process. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. This is from the documentation of the class: I am afraid that this is a well-known bug/feature of the FileSystemWatcher class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |