|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FileSystemWatcher.WaitForChanged surprising (wrong?) behaviourusing (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 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); } } } -- Show quoteDave Sexton "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 In article <uVjo$rbDHHA.1***@TK2MSFTNGP02.phx.gbl>, dave@jwa
[remove.this]online.com says... > Hi John, (snip)> > 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: Thank you very much! I've reported the problem <https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx? FeedbackID=240502> -- Cheers, John
Other interesting topics
DataAdapter.Update bug updating Child table via Relations / RowState corrupted?
ClickOnce program fails to run properly after update Raw TCP/IP library? Trace.WriteLine(object value) - causes "System.InvalidOperationException: Collection was modified; e Other printing problem (resolution not honored) |
|||||||||||||||||||||||