How does this FileSystemWatcher code work?
-
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
-
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
The
FileSystemWatcher
instance is not collected by the garbage collector because of the event handler that is registered to theChanged
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
-
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
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
-
The
FileSystemWatcher
instance is not collected by the garbage collector because of the event handler that is registered to theChanged
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
Thank you for your response. It was helpful. Matt
-
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
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.
-
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.