Home All Groups Group Topic Archive Search About

FileSystemWatcher.WaitForChanged surprising (wrong?) behaviour

Author
21 Nov 2006 2:10 PM
John Aldridge
I'm trying to use the System.IO.FileSystemWatcher class in code like...

   using (FileSystemWatcher changeMonitor = new FileSystemWatcher("."))
   {
       for (; ; )
       {
           WaitForChangedResult c = changeMonitor.WaitForChanged(
               WatcherChangeTypes.Created, 60 * 1000);

           Console.WriteLine ("Creation");
       }
   }

and the code works as expected unless a file change /other/ than a
creation occurs in the folder, at which point the WaitForChanged call no
longer returns even after another creation.

So, in an empty folder

  echo >a.txt   ("Creation" is printed)
  echo >b.txt   ("Creation" is printed)
  dele b.txt    (nothing happens, as expected)
  echo >c.txt   (** nothing happens ... why? **)

Is this the intended behaviour? It seems rather surprising!

I note that if I change the code to pass WatcherChangeTypes.All, then
the code works as expected, but I get notification of changes I'm not
interested in.

--
Cheers,
John

Author
21 Nov 2006 10:17 PM
Dave Sexton
Hi John,

I was able to repro that behavior and it seems like a bug to me.  You should
think about notifying Microsoft :)

Here's a work-around for you in the mean-time:

// 2.0 framework code:

using (FileSystemWatcher changeMonitor = new FileSystemWatcher("."))
{
    System.Threading.EventWaitHandle wait =
        new System.Threading.EventWaitHandle(false,
            System.Threading.EventResetMode.AutoReset);

    string createdFile = null;

    changeMonitor.EnableRaisingEvents = true;
    changeMonitor.Created +=
        delegate(object sender, FileSystemEventArgs e)
    {
        createdFile = e.FullPath;
        wait.Set();
    };

    while (true)        // personal preference ;)
    {
        if (wait.WaitOne(60 * 1000, false))
        {
            Console.WriteLine(createdFile);
        }
    }
}

--
Dave Sexton

Show quote
"John Aldridge" <no.spam@jjdash.demon.co.uk> wrote in message
news:MPG.1fcd1935d72afbf59896ae@news.demon.co.uk...
> I'm trying to use the System.IO.FileSystemWatcher class in code like...
>
>   using (FileSystemWatcher changeMonitor = new FileSystemWatcher("."))
>   {
>       for (; ; )
>       {
>           WaitForChangedResult c = changeMonitor.WaitForChanged(
>               WatcherChangeTypes.Created, 60 * 1000);
>
>           Console.WriteLine ("Creation");
>       }
>   }
>
> and the code works as expected unless a file change /other/ than a
> creation occurs in the folder, at which point the WaitForChanged call no
> longer returns even after another creation.
>
> So, in an empty folder
>
>  echo >a.txt   ("Creation" is printed)
>  echo >b.txt   ("Creation" is printed)
>  dele b.txt    (nothing happens, as expected)
>  echo >c.txt   (** nothing happens ... why? **)
>
> Is this the intended behaviour? It seems rather surprising!
>
> I note that if I change the code to pass WatcherChangeTypes.All, then
> the code works as expected, but I get notification of changes I'm not
> interested in.
>
> --
> Cheers,
> John
Author
24 Nov 2006 1:07 AM
John Aldridge
In article <uVjo$rbDHHA.1***@TK2MSFTNGP02.phx.gbl>, dave@jwa
[remove.this]online.com says...
> Hi John,
>
> I was able to repro that behavior and it seems like a bug to me.  You should
> think about notifying Microsoft :)
>
> Here's a work-around for you in the mean-time:

(snip)

Thank you very much!

I've reported the problem
<https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?
FeedbackID=240502>

--
Cheers,
John

AddThis Social Bookmark Button