472,342 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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

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
4 3900
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FHKBaker | last post by:
I was wondering if anyone has done this and how. I want to be able to pass a parameter through a URL and based upon the URL that is retrieved...
3
by: dana lees | last post by:
Hi, I'm writing a C# asp.net web application. I have a frameset in which the upper frame contains a "print" button and the lower frame...
3
by: rmorvay | last post by:
I allow users to click a datagrid row and it spawns another browser page to allow edits to the data in that row. Once they update the data, it is...
6
by: romy | last post by:
Hi I have two .NET applications that run together on the same local PC. I have a situation, that when I click a button on the first...
1
by: tom c | last post by:
In ASP.net is there any way to trigger an event when you change the selected value of a drop down list box?
1
by: Sam Kong | last post by:
Hi, I want to trigger an event handler when a property changes. If the property is set via a function, it's simple to trigger the attached event...
2
by: frank.elk | last post by:
Hello I would like to trigger an event from within my code. I have a MouseUp event handler and I need to call a paint event handler from within...
1
by: scarng | last post by:
I have a form that has multiple tabs. Each tab contains specific information for the record chosen. One Tab has a function that returns the...
0
by: Chocolade | last post by:
Ok here are the codes of the two events. In the second event im using int veriable to count each time the event is raised. The problem is that when...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.