Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How does this FileSystemWatcher code work?

How does this FileSystemWatcher code work?

Scheduled Pinned Locked Moved C#
questionworkspace
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matthew Cuba
    wrote on last edited by
    #1

    Hello, I am implementing a FileSystemWatcher to watch a directory for changes. The code that I wrote was as follows:

    private void setupDirWatcher(string thePath)
    {
    FileSystemWatcher aWatcher = new FileSystemWatcher();
    aWatcher.Path = thePath;
    aWatcher.NotifyFilter = NotifyFilters.LastWrite;
    aWatcher.Changed += new FileSystemEventHandler(aWatcher_Changed);
    aWatcher.EnableRaisingEvents = true;
    }

    This is called once to do the setup and the intent was to have a watcher on a particular directory. But I don't understand why this would ever work. It would seem to me that since aWatcher is a local variable and should go away at the end of the method, then the watcher shouldn't really work. I realize that 'new' is called here, but I don't see how this has any lifespan outside this method. What I see is that the event handler gets called several times, as changes happen in the directory. I would have expected that aWatcher would need to be a class member for this to work. Is it just a coincidence that this works or am I missing something here? Thanks, Matt

    S S 2 Replies Last reply
    0
    • M Matthew Cuba

      Hello, I am implementing a FileSystemWatcher to watch a directory for changes. The code that I wrote was as follows:

      private void setupDirWatcher(string thePath)
      {
      FileSystemWatcher aWatcher = new FileSystemWatcher();
      aWatcher.Path = thePath;
      aWatcher.NotifyFilter = NotifyFilters.LastWrite;
      aWatcher.Changed += new FileSystemEventHandler(aWatcher_Changed);
      aWatcher.EnableRaisingEvents = true;
      }

      This is called once to do the setup and the intent was to have a watcher on a particular directory. But I don't understand why this would ever work. It would seem to me that since aWatcher is a local variable and should go away at the end of the method, then the watcher shouldn't really work. I realize that 'new' is called here, but I don't see how this has any lifespan outside this method. What I see is that the event handler gets called several times, as changes happen in the directory. I would have expected that aWatcher would need to be a class member for this to work. Is it just a coincidence that this works or am I missing something here? Thanks, Matt

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      The FileSystemWatcher instance is not collected by the garbage collector because of the event handler that is registered to the Changed event.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      M 1 Reply Last reply
      0
      • M Matthew Cuba

        Hello, I am implementing a FileSystemWatcher to watch a directory for changes. The code that I wrote was as follows:

        private void setupDirWatcher(string thePath)
        {
        FileSystemWatcher aWatcher = new FileSystemWatcher();
        aWatcher.Path = thePath;
        aWatcher.NotifyFilter = NotifyFilters.LastWrite;
        aWatcher.Changed += new FileSystemEventHandler(aWatcher_Changed);
        aWatcher.EnableRaisingEvents = true;
        }

        This is called once to do the setup and the intent was to have a watcher on a particular directory. But I don't understand why this would ever work. It would seem to me that since aWatcher is a local variable and should go away at the end of the method, then the watcher shouldn't really work. I realize that 'new' is called here, but I don't see how this has any lifespan outside this method. What I see is that the event handler gets called several times, as changes happen in the directory. I would have expected that aWatcher would need to be a class member for this to work. Is it just a coincidence that this works or am I missing something here? Thanks, Matt

        S Offline
        S Offline
        ShermansLagoon
        wrote on last edited by
        #3

        The life span in C# is a lot different than in C/C++ or other non-garbage collected languages. The "aWatcher" is a pointer to the created instance, and that pointer will expire when the method is finished, while the lifespan if the instance will be as long as, in easy terms, "somebody knows where it is".

        Internet - the worlds biggest dictionary

        M 1 Reply Last reply
        0
        • S Stefan Troschuetz

          The FileSystemWatcher instance is not collected by the garbage collector because of the event handler that is registered to the Changed event.


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          M Offline
          M Offline
          Matthew Cuba
          wrote on last edited by
          #4

          Thank you for your response. It was helpful. Matt

          1 Reply Last reply
          0
          • S ShermansLagoon

            The life span in C# is a lot different than in C/C++ or other non-garbage collected languages. The "aWatcher" is a pointer to the created instance, and that pointer will expire when the method is finished, while the lifespan if the instance will be as long as, in easy terms, "somebody knows where it is".

            Internet - the worlds biggest dictionary

            M Offline
            M Offline
            Matthew Cuba
            wrote on last edited by
            #5

            So in this case, the watcher instance's lifespan is until either the program terminates or I explicitly unregistered the event handler at some point later in the code and I guess that would need to be in the specified callback method itself since I don't actually have a way to get to that watcher instance once the setup method is done. Thanks for the clarification on this.

            D 1 Reply Last reply
            0
            • M Matthew Cuba

              So in this case, the watcher instance's lifespan is until either the program terminates or I explicitly unregistered the event handler at some point later in the code and I guess that would need to be in the specified callback method itself since I don't actually have a way to get to that watcher instance once the setup method is done. Thanks for the clarification on this.

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              Correct. Generally if you need to unregister an event handler you need to save the needed instance information in class variables.

              -- Rules of thumb should not be taken for the whole hand.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups