CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2011
    Location
    .NET Framework version 1.1
    Posts
    1

    filesystem watcher duplicates events

    Hello,

    I wrote a simple watcher that monitors a folder for events (files being changed - they are overwritten via ftp) and when this happens displays a message on the console and runs a matlab script.

    this is part of the code:


    private static void OnChanged(object source, FileSystemEventArgs e)
    {
    Console.WriteLine("File: " + e.Name + " " + e.ChangeType);
    var process = new Process
    {
    StartInfo = new ProcessStartInfo
    {
    FileName = "C:\\Program Files\\MATLAB\\R2011a\\bin\\matlab.bat",
    Arguments = "-r \"prova=1;file\" ";
    }
    };
    process.Start();
    process.WaitForExit();
    }




    public static void Run()
    {
    string pathToWatch = "C:\\Documents and Settings\\aa";
    string[] args = System.Environment.GetCommandLineArgs();
    .
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = pathToWatch;
    watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    watcher.Changed += new FileSystemEventHandler(OnChanged);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

    }



    It works all fine besides the fact that every time a file is changed, it displays it twice on the console and runs matlab twice. There are ways to avoid this, of course, but I was wondering why this happens?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: filesystem watcher duplicates events

    Seems to be a known issue, see: http://bytes.com/topic/c-sharp/answe...esystemwatcher

    Possible workaround: http://spin.atomicobject.com/2010/07...atcher-events/

    Also, please use [code] and [/code] tags around your code; it will preserve formatting and make it more readable.

    Hope that helps!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: filesystem watcher duplicates events

    More likely is the WaitForExit causes the OnChanged handler to block which in tern causes the change evevts to queue up.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: filesystem watcher duplicates events

    Quote Originally Posted by Arjay View Post
    More likely is the WaitForExit causes the OnChanged handler to block which in tern causes the change evevts to queue up.
    Hrm... I am not sure about this. It oughtn't to trigger multiple events for any single file change, so the amount of time it takes to process doesn't seem like it should matter. The articles I referenced seemed to suggest that the duplicate events for the same file where due to FileSystemWatcher monitoring underlying filesystem events which were (for whatever reason) duplicated.

    No?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: filesystem watcher duplicates events

    Quote Originally Posted by BioPhysEngr View Post
    Hrm... I am not sure about this. It oughtn't to trigger multiple events for any single file change, so the amount of time it takes to process doesn't seem like it should matter. The articles I referenced seemed to suggest that the duplicate events for the same file where due to FileSystemWatcher monitoring underlying filesystem events which were (for whatever reason) duplicated.

    No?
    I not saying this is definitely the problem, but if the watcher throws an event and the OnChanged event doesn't immediately return, it's possible that another event will occur while the OnChange event is blocked.

    One way to verify my suggestion is to write some debug output strings inside the OnChange handler (and temporarily remove the blocking process.WaitForExit() call).

    Another possibility is the OnChange handler might be used for multiple change events. If I recall correctly, there is a filter that specifies what types of events that are thrown (i.e. new, changed, renamed, etc. events). If these are wired up to the same handler, you can get what appear as duplicates if the same handler is used.

    Check out the ChangeType property for the FileSystemEventArgs to help narrow down the cause of the change.
    Last edited by Arjay; August 11th, 2011 at 12:21 PM.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: filesystem watcher duplicates events

    I can't say for sure but am thinking that perhaps the reason it fires twice when a file is overwritten could be that it sees the existing file removed then created. I did some work with this object a while back and in some instances it was firing twice. I ended up coding around it so I could get the program out to the end user on time.
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured