Multiple FileSystemWatcher in Global.asax

  • Thread starter Thread starter Antoine Drochon
  • Start date Start date
A

Antoine Drochon

Hi,

I would like to watch on 3 differents files (containing web-application
specific configuration). To do this, i wrote the following code in the
Application_Start of my Global class.

--------8<----------------------------------------8<-------
[C#]

terminalWatcher = new FileSystemWatcher();
sfparamsWatcher = new FileSystemWatcher();
siteconfigWatcher = new FileSystemWatcher();

terminalWatcher.Path = ConfigurationSettings.AppSettings["RessourcePath"];
terminalWatcher.Filter = "terms.xml";
terminalWatcher.Changed += new FileSystemEventHandler(terminalChanged);
terminalWatcher.Created += new FileSystemEventHandler(terminalChanged);
terminalWatcher.Deleted += new FileSystemEventHandler(terminalChanged);
terminalWatcher.EnableRaisingEvents = true;

sfparamsWatcher.Path = ConfigurationSettings.AppSettings["RessourcePath"];
sfparamsWatcher.Filter = "sfparams.xml";
sfparamsWatcher.Changed += new FileSystemEventHandler(sfparamsChanged);
sfparamsWatcher.Created += new FileSystemEventHandler(sfparamsChanged);
sfparamsWatcher.Deleted += new FileSystemEventHandler(sfparamsChanged);
sfparamsWatcher.EnableRaisingEvents = true;

siteconfigWatcher.Path = ConfigurationSettings.AppSettings["RessourcePath"];
siteconfigWatcher.Filter = "sites.xml";
siteconfigWatcher.Changed += new FileSystemEventHandler(siteconfigChanged);
siteconfigWatcher.Created += new FileSystemEventHandler(siteconfigChanged);
siteconfigWatcher.Deleted += new FileSystemEventHandler(siteconfigChanged);
siteconfigWatcher.EnableRaisingEvents = true;

[...]

private static void terminalChanged(
object source, FileSystemEventArgs e)
{
Terminal.ResetTerminal();
return;
}

private static void sfparamsChanged(
object source, FileSystemEventArgs e)
{
// Read the SFParams.xml
ParamCollection.GetCurrent();
}

private static void siteconfigChanged(
object source, FileSystemEventArgs e)
{
// Read the Sites.xml
SiteList.GetCurrent();
}


--------8<----------------------------------------8<-------

But only the first Watcher seems to work. I think this is a threading
issue when entering in the Changed' methods. How to make this 3
FileSystemWatcher work ?

Thank you for your help,
Antoine D.
 
Hi Antoine,

Are you placing all the 3 xml files in same folder ?

Try placing them in different folder.

Not sure whether this works..but you can give a try

Hope this helps you

Thanks
Raghavendra
 
Hi,

It should work, I have never put a FileSystemWatcher in a web app though.
Try this, put the same code in a win app and see what is going on, if it
does not work then you have another problem.


Cheers,
 
Ignacio said:
It should work, I have never put a FileSystemWatcher in a web app though.
Try this, put the same code in a win app and see what is going on, if it
does not work then you have another problem.

I solved this issue (it deals with multhreading i suppose) by using one
FileSystemWatcher : i have only my 3 xml files in this directory, so i
listen to *.xml pattern and i catch the FileSystemEventArgs Name field
in a unique method raised by the watcher.

It works well.

--------8<----------------------------------------8<-------
private static void configfileChanged(
object source, FileSystemEventArgs e)
{
Log.Write("configfileChanged()" + e.Name);
if(e.Name.ToLower() ==
Terminal.CONFIG_FILE.ToLower())
{
Terminal.Reset();
}
else if(e.Name.ToLower() ==
ParamCollection.CONFIG_FILE.ToLower())
{
ParamCollection.GetCurrent();
}
else if(e.Name.ToLower() ==
ConfigSiteList.CONFIG_FILE.ToLower())
{
ConfigSiteList.Reset();
}
}
--------8<----------------------------------------8<-------

Thanks you.

Antoine D.
 
Back
Top