Waiting Until Specified File System Events Occur
This documentation is archived and is not being maintained.

Waiting Until Specified File System Events Occur

In addition to using the FileSystemWatcher component to immediately monitor a specific directory, you can use the WaitForChanged method to wait until a specific event occurs and then continue with execution of the thread. For example, if you are working with a Web-based news application, you might create an admin portion of the site where users upload their news stories. You could use the WaitForChanged method to watch that directory until the last access date changes, and then begin processing the news directory for new articles.

You specify the type of change to watch for by setting the value of a WatcherChangeType enumeration. The possible values are Created, Changed, Renamed, Deleted, or All. Setting the value to All watches for any change to the directory. You can also specify a timeout period if you only want to wait a certain period of time for the event to occur.

The WaitForChanged method returns an object of type WaitForChangedResult. This class contains specific information on the type of change that occurred in the directory. You can access information such as Name, OldName, and TimedOut on this object to find out more about the change.

WaitForChanged is a synchronous method. If you use it in a Windows Form application, the application would stop responding if the method was used on the UI thread instead of the worker thread. For more information on threading issues in Visual Basic .NET, see Multithreading in Visual Basic .NET.

To wait for changes

  1. Create and configure your FileSystemWatcher component instance to indicate the directory you want to watch. For more information, see Creating FileSystemWatcher Component Instances or Configuring FileSystemWatcher Component Instances.
  2. In the Code Editor, create an instance of the WaitForChangedResult class.
    ' Visual Basic
    Dim result As System.IO.WaitForChangedResult
    
    // C#
    System.IO.WaitForChangedResult result;
    
  3. On your FileSystemWatcher component instance, specify the type of event it is waiting for by setting the value of the WaitForChanged property to one of the values of the WatcherChangeTypes enumeration.
    ' Visual Basic
    fileSystemWatcher1.WaitForChanged(System.IO.WatcherChangeTypes.All)
    
    // C#
    fileSystemWatcher1.WaitForChanged(System.IO.WatcherChangeTypes.All);
    
    Tip   You can use an overloaded form of the WaitForChanged method to specify a timeout period as a second parameter.
  4. Specify the actions you want to take when the change for which you are watching occurs. For more information, see Creating Handlers for File System Events.

The following code shows how to notify a user that a file needs to be deleted, then wait for the Deleted event to be raised. This code watches the personal folder of the user.

' Visual Basic
Console.WriteLine("Please delete OldNote.txt.")
Dim directory As String
directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim watcher As New System.IO.FileSystemWatcher(directory, "OldNote.txt")
result = watcher.WaitForChanged(System.IO.WatcherChangeTypes.Deleted)
Console.WriteLine("Thanks for deleting OldNote.txt.")

// C#
Console.WriteLine("Please delete OldNote.txt.");
string directory = 
   Environment.GetFolderPath(Environment.SpecialFolder.Personal);
System.IO.FileSystemWatcher watcher = 
   new System.IO.FileSystemWatcher(directory,"OldNote.txt");
result = watcher.WaitForChanged(System.IO.WatcherChangeTypes.Deleted);
Console.WriteLine("Thanks for deleting OldNote.txt.");

See Also

Creating FileSystemWatcher Component Instances | Configuring FileSystemWatcher Component Instances | Introduction to Monitoring File System Events | Creating Handlers for File System Events

Show:
© 2016 Microsoft