Compartilhar via


How to: Wait 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 WatcherChangeTypes 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 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, see Multithreading in Visual Basic.

To wait for changes

  1. Create and configure your FileSystemWatcher component instance to indicate the directory you want to watch. For more information, see How to: Create FileSystemWatcher Component Instances or How to: Configure FileSystemWatcher Component Instances.

  2. In the Code Editor, create an instance of the WaitForChangedResult class.

    Dim result As System.IO.WaitForChangedResult
    
    System.IO.WaitForChangedResult result;
    
    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.

    fileSystemWatcher1.WaitForChanged(System.IO.WatcherChangeTypes.All)
    
    fileSystemWatcher1.WaitForChanged(System.IO.WatcherChangeTypes.All);
    
    fileSystemWatcher1.WaitForChanged(System.IO.WatcherChangeTypes.All);
    
    NoteTip

    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 How to: Create 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.

    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.")
    
    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.");
    
    Console.WriteLine("Please delete OldNote.txt.");
    System.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

Tasks

How to: Create FileSystemWatcher Component Instances
How to: Configure FileSystemWatcher Component Instances
How to: Create Handlers for File System Events

Concepts

Introduction to Monitoring File System Events