mbramer

mbramer

  • NA
  • 1
  • 0

FileSystemWatcher.WaitForChanged() ???

Feb 12 2004 8:07 PM
Hi all, I have a Windows service that uses the FileSystemWatcher class to watch for new files in a particular folder. I'm having a problem figuring out how to use the WaitForChanged() method to wait until the file is finished being copied. I set up my FileSystemWatcher in my service's startup class, and I register an event handler for the Created event. In my event handler, I cast the 'source' object (the original FSW) and call the WaitForChanged() method. My logic is this: the Changed event fires before the file is finished copying into the folder, but the beginning of my Changed event handler stops and waits for the file to finish copying before continuing. The problem is simple: the WaitForChanged() method never returns. I know there's a WaitForChanged() constructor with a timeout, but this is very sloppy and wreaks havoc on the app if multiple large files are copied in at the same time. Why isn't the WaitForChanged() method returning? It's waiting and waiting. I can't put the call to WaitForChanged() in my services's startup class, because the service will never start elegantly, since it would be waiting for a file change. Here's my code: // This runs when the service is started... public IceDataMonitor() { // This call is required by the Windows.Forms Component Designer. InitializeComponent(); FileSystemWatcher fswCreate; fswCreate = new FileSystemWatcher(); ((System.ComponentModel.ISupportInitialize)(fswCreate)).BeginInit(); fswCreate.Path = @"C:\temp"; fswCreate.IncludeSubdirectories = true; fswCreate.EnableRaisingEvents = true; fswCreate.Created += new FileSystemEventHandler(this.CreatedItem); ((System.ComponentModel.ISupportInitialize)(fswCreate)).EndInit(); } // public IceDataMonitor() private void CreatedItem(object source, FileSystemEventArgs e) { FileSystemWatcher fsw = (FileSystemWatcher) source; WaitForChangedResult result; result = fsw.WaitForChanged(WatcherChangeTypes.Created); // here is where my CreatedItem event handler would continue, but nothing // ever reaches this point because the fsw is still on WaitForChanged() } I've also tried experimenting with "LastAccess" from the NotifyFilters enumeration, since this seems to be the only one that waits until the process is finished. However, I haven't been able to get this to work, either. I apologize for not having the details on why I can't get this to work, either. I gave up on it long ago... I can't thank you enough if you can point me in the right direction... Mark