FileSystemWatchers and CacheDependency

G

Guest

Hi,
I am developing an application that requires me to set up file system
watchers on directories on a network drive, as well as set up
CacheDependencies in the same location for my System.Web.Caching.Cache. When
i set up the file watchers and then create the cache dependency i receive an
Exception (AccessViolationError - Attempted to read or write protected
memory). I have included a the stack trace from the exception at the end of
this post. This only occurs when i am creating a large number of file
watchers ( i have found the the for my PC i can only create 48!). The
following is a sample application i built which consistantly throws the
exception( note that when i copy the directory structe from the network drive
to a local drive the function works flawlessly ).

List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
foreach( string directory in Directory.GetDirectories(
@"\\itchy\imagelink\Books\V3Books" ) )
{
FileSystemWatcher watcher = new FileSystemWatcher( directory );
watcher.Deleted += new FileSystemEventHandler( watcher_Deleted );
watcher.Created += new FileSystemEventHandler( watcher_Created );
watcher.Renamed += new RenamedEventHandler( watcher_Renamed );
watcher.Changed += new FileSystemEventHandler( watcher_Changed );
watcher.EnableRaisingEvents = true;
watchers.Add( watcher );
}
CacheDependency depencency2 = new CacheDependency(
@"\\itchy\imagelink\Books\V3Books\man.lst" );


Partial Stack Trace:

at System.Web.UnsafeNativeMethods.RaiseFileMonitoringEventlogEvent(String
eventInfo, String path, String appVirtualPath, Int32 hr)
at System.Web.FileChangesMonitor.CreateFileMonitoringException(Int32 hr,
String path)
at System.Web.DirMonCompletion..ctor(DirectoryMonitor dirMon, String dir,
Boolean watchSubtree, UInt32 notifyFilter)
at System.Web.DirectoryMonitor.StartMonitoring()
at System.Web.DirectoryMonitor.StartMonitoringFile(String file,
FileChangeEventHandler callback, String alias)
at System.Web.FileChangesMonitor.StartMonitoringPath(String alias,
FileChangeEventHandler callback, FileAttributesData& fad)
at System.Web.Caching.CacheDependency.Init(Boolean isPublic, String[]
filenamesArg, String[] cachekeysArg, CacheDependency dependency, DateTime
utcStart)
at System.Web.Caching.CacheDependency..ctor(String filename, DateTime start)
at System.Web.Caching.CacheDependency..ctor(String filename)
at WindowsApplication4.Form1.LoadTargetFolder() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Form1.cs:line 52
at WindowsApplication4.Program.Main() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Thanks,
Daniel
 
G

Guest

daniel,

Not really answering your question but I am wondering why you don't just
create one FileSystemWatcher on "\\itchy\imagelink\Books\V3Books" and set the
IncludeSubdirectories property?

Another option might be to map a drive to "\\itchy\imagelink\Books\V3Books"
and then enumerate the directories through the mapped drive.

Michael

daniel said:
Hi,
I am developing an application that requires me to set up file system
watchers on directories on a network drive, as well as set up
CacheDependencies in the same location for my System.Web.Caching.Cache. When
i set up the file watchers and then create the cache dependency i receive an
Exception (AccessViolationError - Attempted to read or write protected
memory). I have included a the stack trace from the exception at the end of
this post. This only occurs when i am creating a large number of file
watchers ( i have found the the for my PC i can only create 48!). The
following is a sample application i built which consistantly throws the
exception( note that when i copy the directory structe from the network drive
to a local drive the function works flawlessly ).

List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
foreach( string directory in Directory.GetDirectories(
@"\\itchy\imagelink\Books\V3Books" ) )
{
FileSystemWatcher watcher = new FileSystemWatcher( directory );
watcher.Deleted += new FileSystemEventHandler( watcher_Deleted );
watcher.Created += new FileSystemEventHandler( watcher_Created );
watcher.Renamed += new RenamedEventHandler( watcher_Renamed );
watcher.Changed += new FileSystemEventHandler( watcher_Changed );
watcher.EnableRaisingEvents = true;
watchers.Add( watcher );
}
CacheDependency depencency2 = new CacheDependency(
@"\\itchy\imagelink\Books\V3Books\man.lst" );


Partial Stack Trace:

at System.Web.UnsafeNativeMethods.RaiseFileMonitoringEventlogEvent(String
eventInfo, String path, String appVirtualPath, Int32 hr)
at System.Web.FileChangesMonitor.CreateFileMonitoringException(Int32 hr,
String path)
at System.Web.DirMonCompletion..ctor(DirectoryMonitor dirMon, String dir,
Boolean watchSubtree, UInt32 notifyFilter)
at System.Web.DirectoryMonitor.StartMonitoring()
at System.Web.DirectoryMonitor.StartMonitoringFile(String file,
FileChangeEventHandler callback, String alias)
at System.Web.FileChangesMonitor.StartMonitoringPath(String alias,
FileChangeEventHandler callback, FileAttributesData& fad)
at System.Web.Caching.CacheDependency.Init(Boolean isPublic, String[]
filenamesArg, String[] cachekeysArg, CacheDependency dependency, DateTime
utcStart)
at System.Web.Caching.CacheDependency..ctor(String filename, DateTime start)
at System.Web.Caching.CacheDependency..ctor(String filename)
at WindowsApplication4.Form1.LoadTargetFolder() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Form1.cs:line 52
at WindowsApplication4.Program.Main() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Thanks,
Daniel
 
G

Guest

Michael,

The number of file watchers that causes the crash seems to be completely
intermitant, my example may be a bit overly complex but i hav also tried
having just one file watcher on the network directory and some times it
crashes sometimes it doesn't. It also seems to make no difference whether it
is a mapped network drive or not.

Also, the number of filewatchers differs between locations that i map to..
on some network paths i can create several file watchers before a crash, on
other servers only one...

I have tried a mapping to servers with various operating systems including
windows xp, win 2000 and 2003 all with the same results.

Regards
Daniel

Michael said:
daniel,

Not really answering your question but I am wondering why you don't just
create one FileSystemWatcher on "\\itchy\imagelink\Books\V3Books" and set the
IncludeSubdirectories property?

Another option might be to map a drive to "\\itchy\imagelink\Books\V3Books"
and then enumerate the directories through the mapped drive.

Michael

daniel said:
Hi,
I am developing an application that requires me to set up file system
watchers on directories on a network drive, as well as set up
CacheDependencies in the same location for my System.Web.Caching.Cache. When
i set up the file watchers and then create the cache dependency i receive an
Exception (AccessViolationError - Attempted to read or write protected
memory). I have included a the stack trace from the exception at the end of
this post. This only occurs when i am creating a large number of file
watchers ( i have found the the for my PC i can only create 48!). The
following is a sample application i built which consistantly throws the
exception( note that when i copy the directory structe from the network drive
to a local drive the function works flawlessly ).

List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
foreach( string directory in Directory.GetDirectories(
@"\\itchy\imagelink\Books\V3Books" ) )
{
FileSystemWatcher watcher = new FileSystemWatcher( directory );
watcher.Deleted += new FileSystemEventHandler( watcher_Deleted );
watcher.Created += new FileSystemEventHandler( watcher_Created );
watcher.Renamed += new RenamedEventHandler( watcher_Renamed );
watcher.Changed += new FileSystemEventHandler( watcher_Changed );
watcher.EnableRaisingEvents = true;
watchers.Add( watcher );
}
CacheDependency depencency2 = new CacheDependency(
@"\\itchy\imagelink\Books\V3Books\man.lst" );


Partial Stack Trace:

at System.Web.UnsafeNativeMethods.RaiseFileMonitoringEventlogEvent(String
eventInfo, String path, String appVirtualPath, Int32 hr)
at System.Web.FileChangesMonitor.CreateFileMonitoringException(Int32 hr,
String path)
at System.Web.DirMonCompletion..ctor(DirectoryMonitor dirMon, String dir,
Boolean watchSubtree, UInt32 notifyFilter)
at System.Web.DirectoryMonitor.StartMonitoring()
at System.Web.DirectoryMonitor.StartMonitoringFile(String file,
FileChangeEventHandler callback, String alias)
at System.Web.FileChangesMonitor.StartMonitoringPath(String alias,
FileChangeEventHandler callback, FileAttributesData& fad)
at System.Web.Caching.CacheDependency.Init(Boolean isPublic, String[]
filenamesArg, String[] cachekeysArg, CacheDependency dependency, DateTime
utcStart)
at System.Web.Caching.CacheDependency..ctor(String filename, DateTime start)
at System.Web.Caching.CacheDependency..ctor(String filename)
at WindowsApplication4.Form1.LoadTargetFolder() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Form1.cs:line 52
at WindowsApplication4.Program.Main() in
D:\MyProjects\WindowsApplication4\WindowsApplication4\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Thanks,
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top