473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3992
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 trigger an event. For instance: 1. pass...
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 contains content of a different domain. Since i cannot...
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 committed to the database and the form is closed. ...
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 application, I need to trigger an event for the second...
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 handler. But if the property is set directly, I...
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 the former, to update the form of my application....
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 distinct record from another table based on the initial...
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 the first event is raised also the second one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.