How to turn off file watcher 
Author Message
 How to turn off file watcher

I have the following code:
The code works fine, except that I can not turn off the FileSystemWatcher events once they are turned on.  I have tried to issue watchFile.EnableRaisingEvents = true; but the events keep on firing.  How can I turned them off?

public void Monitor()
{
 WaitForChangedResult result;
 string filePath = "C:\\Test", fileMask = "*.*";
 //
 // create watch event
 //
 watchFile = new FileSystemWatcher(filePath, fileMask);
 watchFile.Created += new FileSystemEventHandler(this.FileCreated);
 watchFile.Deleted += new FileSystemEventHandler(this.FileDeleted);
 watchFile.Changed += new FileSystemEventHandler(this.FileCreated);
 watchFile.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
 watchFile.IncludeSubdirectories = true;
 watchFile.EnableRaisingEvents = true;

 try
 {
  while(true)
  {
   //this will put the thread in
   //a Wait / Sleep state waiting for notifications
   result = watchFile.WaitForChanged(WatcherChangeTypes.Changed | WatcherChangeTypes.Created | WatcherChangeTypes.Deleted);  }
 }
 catch
 {}

Quote:
}

Peter

Thanks
-------------------



Tue, 07 Jun 2005 06:35:50 GMT  
 How to turn off file watcher
Hello Peter,

You may remove the Event Handlers to disable the events. Please Note: if
you perform these actions within the same thread of your listening thread
(in which you call the WaitForChanged method), you need to give a chance to
call other method to remove the handler. This is a sample code:

private void buttonAdd_Click(object sender, System.EventArgs e)
{
        WaitForChangedResult result;
        string filePath = "C:\\Test", fileMask = "*.*";
        //
        // create watch event
        //
        watchFile = new FileSystemWatcher(filePath, fileMask);
        watchFile.Created += new FileSystemEventHandler(this.FileCreated);
        watchFile.Deleted += new FileSystemEventHandler(this.FileDeleted);
        watchFile.Changed += new FileSystemEventHandler(this.FileCreated);
        watchFile.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName;
        watchFile.IncludeSubdirectories = true;
        watchFile.EnableRaisingEvents = true;
        try
        {
                while(true)
                {
                        //this will put the thread in
                        //a Wait / Sleep state waiting for notifications
                        result = watchFile.WaitForChanged(WatcherChangeTypes.Changed |
WatcherChangeTypes.Created | WatcherChangeTypes.Deleted, 50);

                        //Process Messages here
                        Application.DoEvents();
                }
        }
        catch
        {}

Quote:
}

private void buttonRemove_Click(object sender, System.EventArgs e)
{
        watchFile.Created -= new FileSystemEventHandler(this.FileCreated);
        watchFile.Deleted -= new FileSystemEventHandler(this.FileDeleted);
        watchFile.Changed -= new FileSystemEventHandler(this.FileCreated);

Quote:
}

I hope this helps you.

Best regards,

Lion Shi [MSFT]
MCSE, MCSD
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------

    Subject: How to turn off file watcher
    Date: Thu, 19 Dec 2002 16:35:50 -0600
    Lines: 119
    MIME-Version: 1.0
    Content-Type: multipart/alternative;
        boundary="----=_NextPart_000_000D_01C2A77C.B11BAF00"
    X-Priority: 3
    X-MSMail-Priority: Normal
    X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
    X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106

    Newsgroups: microsoft.public.dotnet.languages.CSharp
    NNTP-Posting-Host: new-24-166-130-53.new.rr.com 24.166.130.53
    Path: cpmsftngxa06!TK2MSFTNGP08!TK2MSFTNGP09
    Xref: cpmsftngxa06 microsoft.public.dotnet.languages.csharp:117924
    X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

    I have the following code:
    The code works fine, except that I can not turn off the
FileSystemWatcher events once they are turned on.  I have tried to issue
watchFile.EnableRaisingEvents = true; but the events keep on firing.  How
can I turned them off?
    public void Monitor()
    {
     WaitForChangedResult result;
     string filePath = "C:\\Test", fileMask = "*.*";
     //
     // create watch event
     //
     watchFile = new FileSystemWatcher(filePath, fileMask);
     watchFile.Created += new FileSystemEventHandler(this.FileCreated);
     watchFile.Deleted += new FileSystemEventHandler(this.FileDeleted);
     watchFile.Changed += new FileSystemEventHandler(this.FileCreated);
     watchFile.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName;
     watchFile.IncludeSubdirectories = true;
     watchFile.EnableRaisingEvents = true;
     try
     {
      while(true)
      {
       //this will put the thread in
       //a Wait / Sleep state waiting for notifications
       result = watchFile.WaitForChanged(WatcherChangeTypes.Changed |
WatcherChangeTypes.Created | WatcherChangeTypes.Deleted);  }
     }
     catch
     {}
    }
    Peter
    Thanks
    -------------------



Fri, 10 Jun 2005 13:44:59 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Ways of turning echo off ... off

2. Power On/Off and Turn off screen?

3. Turning off the warnings when generating a doc comments XML file

4. File System Watcher

5. turn off debugging slop area in VC++ IDE ?

6. turning off run time checks

7. Is there a way to turn off error handling in C#

8. How do I turn off the TaskList

9. Turning off or tracking specific optimizations

10. Using File Watcher

11. How do I turn off all form and mouse cursor updates

12. turn off my Computer

 

 
Powered by phpBB® Forum Software