By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

FileSystemWatcher will trigger more than one event for updating a xml file,I only need one!!

P: n/a
Dear all;
I need to monitor a xml file so that I can update my data in
hashtable, but the problem is it will trigger more than one event for
a lastwrite action(I trigger this action by add something in the xml
file using notepad).
Here is my codes about using FilesystemWatcher

fileWatcher=new FileSystemWatcher();
fileWatcher.Path=System.Environment.CurrentDirecto ry;
fileWatcher.NotifyFilter=NotifyFilters.LastWrite;
fileWatcher.Filter="SACHandlers.xml";
fileWatcher.Changed+=new FileSystemEventHandler(OnChanged);
fileWatcher.IncludeSubdirectories=false;
fileWatcher.EnableRaisingEvents = true;

And here is the OnChanged code

Monitor.Enter(this);
//_reentryLock=true;
Hashtable _ht = null;
string msg="";
DateTime _dt= DateTime.Now;
ReportMessage(_lastTriggerTime.ToString());
log.Info("lasttriggertime:" + _lastTriggerTime.ToString());
ReportMessage(_dt.ToString());
log.Info("dt time:" + _dt.ToString());
TimeSpan sp = _dt.Subtract( _lastTriggerTime);
ReportMessage(sp.Seconds.ToString() );
log.Info("sp:" + sp.Seconds.ToString());
if(sp.Seconds>30)
{
_lastTriggerTime=DateTime.Now;
_ht=new Hashtable();
log.Info("SACHandlers.xml file update!!");
try
{
_ht=ReloadSacInfo();
}
catch(Exception ex)
{
msg="Reload Sac info from xml fail!!" + ex.Message;
// _reentryLock=false;
log.Error(msg);
ReportMessage(msg);
//_cnt=0;
}
//_cnt=0;
}

//_reentryLock=false;
Monitor.Exit(this);
No matter what way I use it will trigger twice even using
lock,Monitor,timespan check,lock variable .. The IDE will run the same
code twice(is it because they are triggered almost at the same time?so
monitor,lock is useless here?) when you step over in debug mode...
This drives me crazy.
So far, I think I need to use a flag and a timer to check, I cannot do
anything in OnChanged method, I think I only need to put a
isFileChanged flag there and use a timer to check that flag, if it
changes, I will do something....
Too bad solution, Could someone here have a better solution?

Thanks....
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Peter,

If you search in the archives you will see a lot of post where this
"feature" is described, a Changed event is raised more than one when you
change a doc.
The solution depends of your escenario, I will describe mine.
I use a DataTime to prevent too frequents updates, if I see that the
TimeSpan between the last event and the current one is less than a
predefined amount I ignore the event.

AFAIK there is no way to know for sure if two separates events are part of
the same "user operation" or not, this is due that an operation that for a
user is a unique event in the OS it's split in several simpler ones.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Lin" <no**************@spirox.com.tw> wrote in message
news:bf********************************@4ax.com...
Dear all;
I need to monitor a xml file so that I can update my data in
hashtable, but the problem is it will trigger more than one event for
a lastwrite action(I trigger this action by add something in the xml
file using notepad).
Here is my codes about using FilesystemWatcher

fileWatcher=new FileSystemWatcher();
fileWatcher.Path=System.Environment.CurrentDirecto ry;
fileWatcher.NotifyFilter=NotifyFilters.LastWrite;
fileWatcher.Filter="SACHandlers.xml";
fileWatcher.Changed+=new FileSystemEventHandler(OnChanged);
fileWatcher.IncludeSubdirectories=false;
fileWatcher.EnableRaisingEvents = true;

And here is the OnChanged code

Monitor.Enter(this);
//_reentryLock=true;
Hashtable _ht = null;
string msg="";
DateTime _dt= DateTime.Now;
ReportMessage(_lastTriggerTime.ToString());
log.Info("lasttriggertime:" + _lastTriggerTime.ToString());
ReportMessage(_dt.ToString());
log.Info("dt time:" + _dt.ToString());
TimeSpan sp = _dt.Subtract( _lastTriggerTime);
ReportMessage(sp.Seconds.ToString() );
log.Info("sp:" + sp.Seconds.ToString());
if(sp.Seconds>30)
{
_lastTriggerTime=DateTime.Now;
_ht=new Hashtable();
log.Info("SACHandlers.xml file update!!");
try
{
_ht=ReloadSacInfo();
}
catch(Exception ex)
{
msg="Reload Sac info from xml fail!!" + ex.Message;
// _reentryLock=false;
log.Error(msg);
ReportMessage(msg);
//_cnt=0;
}
//_cnt=0;
}

//_reentryLock=false;
Monitor.Exit(this);
No matter what way I use it will trigger twice even using
lock,Monitor,timespan check,lock variable .. The IDE will run the same
code twice(is it because they are triggered almost at the same time?so
monitor,lock is useless here?) when you step over in debug mode...
This drives me crazy.
So far, I think I need to use a flag and a timer to check, I cannot do
anything in OnChanged method, I think I only need to put a
isFileChanged flag there and use a timer to check that flag, if it
changes, I will do something....
Too bad solution, Could someone here have a better solution?

Thanks....

Nov 16 '05 #2

P: n/a
Dear Sir,
Big thanks for your help....
I just use your way to prevent second update event, but my problem is
it almost arrived at the same time, and it is quite difficult to debug
in vs.net environment because reentry issue.I have something important
need to be done in onChanged method, I cannot handle the reentry issue
..Could you tell me how you handle the reentry issue? I have searched
the google for this issue..I do know the update will trigger more than
one event. I just don't know how to handle reentry issue, I just want
that part of codes to be run once when a change event arrives.
Hi Peter,

If you search in the archives you will see a lot of post where this
"feature" is described, a Changed event is raised more than one when you
change a doc.
The solution depends of your escenario, I will describe mine.
I use a DataTime to prevent too frequents updates, if I see that the
TimeSpan between the last event and the current one is less than a
predefined amount I ignore the event.

AFAIK there is no way to know for sure if two separates events are part of
the same "user operation" or not, this is due that an operation that for a
user is a unique event in the OS it's split in several simpler ones.

Cheers,


Nov 16 '05 #3

P: n/a
Hi Peter,

You can use a lock statement to isolate your code from another thread.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Lin" <no**************@spirox.com.tw> wrote in message
news:eh********************************@4ax.com...
Dear Sir,
Big thanks for your help....
I just use your way to prevent second update event, but my problem is
it almost arrived at the same time, and it is quite difficult to debug
in vs.net environment because reentry issue.I have something important
need to be done in onChanged method, I cannot handle the reentry issue
.Could you tell me how you handle the reentry issue? I have searched
the google for this issue..I do know the update will trigger more than
one event. I just don't know how to handle reentry issue, I just want
that part of codes to be run once when a change event arrives.
Hi Peter,

If you search in the archives you will see a lot of post where this
"feature" is described, a Changed event is raised more than one when you
change a doc.
The solution depends of your escenario, I will describe mine.
I use a DataTime to prevent too frequents updates, if I see that the
TimeSpan between the last event and the current one is less than a
predefined amount I ignore the event.

AFAIK there is no way to know for sure if two separates events are part ofthe same "user operation" or not, this is due that an operation that for auser is a unique event in the OS it's split in several simpler ones.

Cheers,

Nov 16 '05 #4

P: n/a
Dear Sir,
I will try it..
Thanks..
Hi Peter,

You can use a lock statement to isolate your code from another thread.

Cheers,


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.