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

P

Peter Lin

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.CurrentDirectory;
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....
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 
P

Peter Lin

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Peter,

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

Cheers,
 
Joined
Jul 17, 2007
Messages
1
Reaction score
0
FileSystemWatcherdetects file first change, but not tte second

Hello,

Could anyone help me with the following issue?

I am using FileSystemWatcher to detect web.config file changes in a web service application.
When my web service is up, I change the web.config file and save it, the fileSytemWatcher.Changed evet is raised. But the second time I open the file and do the changes, the event ,is not raised. What is the reason for it?

Thanks a lot
Fiori

The codde is below:

public FileSystemWatcher fileWatcher = new FileSystemWatcher();


private
void InitializeComponent()
{

this.components = new System.ComponentModel.Container();





fileWatcher.Path =
ConfigurationManager.AppSettings["RESOURCE_PATH"];

fileWatcher.Filter =
"Web.config";

fileWatcher.NotifyFilter =
NotifyFilters.LastWrite;

fileWatcher.EnableRaisingEvents =
true;

fileWatcher.Changed +=
new FileSystemEventHandler(FileChanged);





}

public void FileChanged(Object obj, FileSystemEventArgs e)

{

if (e.ChangeType == WatcherChangeTypes.Changed)

{

ConfigurationManager.RefreshSection("appSettiings");

string sUser = ConfigurationManager.AppSettings ["ERROR_ALERT_TYPE"].ToString();

}



}

 
Top