Jonas Rembratt
Jonas Rembratt

Reputation: 1752

FileSystemWatcher causes crash to desktop

I'm writing a solution where I use some configuration files that should be editable at runtime. I've been using FileSystemWatcher for this purpose before and never had much issues with it but now it's causing a CTD on the 'rename' event.

This (useless) piece of code will recreate the problem in my setup:

private static int _s_renamed;
private static int _s_created;
private static int _s_errors;

private static void monitorConfiguration(string configRootFolder)
{
    var fsw = new FileSystemWatcher(configRootFolder, ConfigFilePattern)
    {
        NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName,
        IncludeSubdirectories = false
    };
    fsw.Renamed += (sender, args) => ++_s_renamed; // <-- ! CTD efter this one !
    fsw.Created += (sender, args) => ++_s_created;
    fsw.Error += (sender, args) => ++_s_errors; 
    fsw.EnableRaisingEvents = true;
}

The crash comes from FileSystemWatcher it seems. If I set a breakpoint in the event handler for FileSystemWatcher.Renamed it gets hit but the app crashes when I step out of it. If I set a breakpoint in the FileSystemWatcher.Created event handler this does not happen.

Any suggestions?


EDIT 1: I'm running .NET 4 on a Windows 7 x64 (Ultimate) platform I have seen several discussions concerning this type of problems but all has been related to people trying to update UI stuff (which must be done from the main/UI thread) from the event handlers. That's why I just try to increment some counters in the experimental code.

Upvotes: 11

Views: 2864

Answers (4)

user1031246
user1031246

Reputation: 1

Maybe your file or object are in use, I had a similar problem and I resolved it using the following code

private void InitWatch()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"C:\LoQueSea";
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
    | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.*";
    watcher.Created += new FileSystemEventHandler(OnCreated);
    watcher.EnableRaisingEvents = true;
}
private void OnCreated()
{
    try
    {
        if (!myObjectToPrint.Dispatcher.CheckAccess())
        {
            myObjectToPrint.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                   delegate()
                   {
                    //your code here...
                   }
                   )
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }          
}

Saludos..

Upvotes: 0

Jonas Rembratt
Jonas Rembratt

Reputation: 1752

Just to clarify:

The problem here was I had more/older consumers of the FileSystemWatcher elsewhere in my system and one of them caused an unhandled exception. The problem is the exception gets thrown in a completely different thread and caused the application to crasch to desktop. The timing fooled me into thinking it was my new consumer that somehow cause the isse but when I followed Chris Shain's advice (see comments in the question entry) to enable break on exceptions (msdn.microsoft.com/en-us/library/d14azbfh.aspx) I immediately found the real culprit.

I would have preferred to credit Chris with the solution but he never re-posted so here it is. Hopefully we've learned something.

Thanks everyone and happy coding

/Jonas

Upvotes: 1

Jos&#233; Cruz
Jos&#233; Cruz

Reputation: 35

In .NET you must sync the thread generated by FileSystemWatcher with the UI Thread. For this, the UI controls have a method like: myControl.Invoke(...) for this effect. Any other way to try to sync will have some random effects like crashs, Exceptions, etc.

see here: http://msdn.microsoft.com/en-us/magazine/cc300429.aspx http://weblogs.asp.net/justin_rogers/pages/126345.aspx

hope it helps

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31721

You may be running into any of these three situations

  1. The directory passed in does not exist and an file io not found is being thrown.
  2. The directory passed in is valid but the process running it does not have access rights
  3. The arguments (sender, args) as passed in, one may be null and your code (since this is an example and we can't see the real code) is not handling the null and throwing an error.

Upvotes: 0

Related Questions