vb.net - FileSystemWatcher - Reduce Multiple Event Raising (or Reduce Results of Multiple Events) in Visual Basic (VS 2012 V11) -
i'm trying reduce number of events raised 1 when filesystemwatcher
changes (or @ least display text in textbox once when event raised, either removing duplicate text or other method).
is there fix in visual basic, or @ least anyway remove duplicated text have being appended status log textbox (when onchange
sub called)?
i've done decent amount of research on , looks there no "simple" fix.
here list of resources:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx (the microsfot documentation includes notification filesystemwatcher may raise more 1 event.)
http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx (an article states, "to correct problem [two events being raised], remove explicit event handler (addhandler ...)." there no further explanation "...remove explicit event handler (addhandler ...)." means.)
why filesystemwatcher fire twice (this question asked states "what "remove explicit event handler" mean?" no 1 answered question. also, answer given has portion of code:
private sub filesystemwatcher1_changed(byval sender system.object, _ byval e system.io.filesystemeventargs) handles filesystemwatcher1.changed
when try blue lines under filesystemwatcher1 , this:
handles clause requires withevents variable defined in containing type or 1 of base types.
i tried similar fix, setting
enableraisingevents = false
inonchanged sub
, having program need , settingenableraisingevents = true
. seems work, sometimes. believe there race condition happening here.enableraisingevents
not set false fast enough , multipleonchanges
called leading duplicated results.http://spin.atomicobject.com/2010/07/08/consolidate-multiple-filesystemwatcher-events (a rather involved fix , in c# , can understand of it, need see in visual basic.)
http://www.codeproject.com/articles/58740/filesystemwatcher-pure-chaos-part-1-of-2 (this 2 part article in c#, seems solid fix. although lengthy , don't understand of not in visual basic.)
additional information:
private sub formbackupfile_load(byval sender system.object, byval e _ system.eventargs) handles mybase.load dim varfilesystemwatcher new filesystemwatcher() dim directorypath string = path.getdirectoryname(textboxfilelocation.text) varfilesystemwatcher.synchronizingobject = me varfilesystemwatcher.path = directorypath varfilesystemwatcher.notifyfilter = (notifyfilters.lastwrite) ' event raised twice or more addhandler varfilesystemwatcher.changed, addressof onchanged varfilesystemwatcher.enableraisingevents = true end sub private sub onchanged(source object, e filesystemeventargs) ' stops filesystemwatcher firing twice or more but, ' works @ random. varfilesystemwatcher.enableraisingevents = false ' copies specified file specified location my.computer.filesystem.copyfile(e.fullpath, textboxcopyto.text & "\" _ & e.name, true) ' text added textbox twice or more. textboxstatuslog.appendtext(environment.newline & environment.newline & _ statusupdate) ' starts filesystemwatcher again varfilesystemwatcher.enableraisingevents = true end sub
Comments
Post a Comment