Click here to Skip to main content
15,913,486 members
Home / Discussions / C#
   

C#

 
AnswerSparse Files Pin
Luc Pattyn5-Feb-07 9:27
sitebuilderLuc Pattyn5-Feb-07 9:27 
Questionrichtextbox Pin
sahelearamesh5-Feb-07 2:22
sahelearamesh5-Feb-07 2:22 
AnswerRe: richtextbox Pin
quiteSmart5-Feb-07 2:56
quiteSmart5-Feb-07 2:56 
GeneralRe: richtextbox Pin
netJP12L5-Feb-07 5:58
netJP12L5-Feb-07 5:58 
AnswerRe: richtextbox Pin
Ravi Bhavnani5-Feb-07 7:33
professionalRavi Bhavnani5-Feb-07 7:33 
AnswerRe: richtextbox Pin
Pratik Vasant Shah5-Feb-07 20:11
Pratik Vasant Shah5-Feb-07 20:11 
AnswerRe: richtextbox Pin
VirtualVoid.NET6-Feb-07 3:39
VirtualVoid.NET6-Feb-07 3:39 
QuestionHow does this FileSystemWatcher code work? Pin
Matthew Cuba5-Feb-07 2:13
Matthew Cuba5-Feb-07 2:13 
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
AnswerRe: How does this FileSystemWatcher code work? Pin
Stefan Troschuetz5-Feb-07 3:16
Stefan Troschuetz5-Feb-07 3:16 
GeneralRe: How does this FileSystemWatcher code work? Pin
Matthew Cuba5-Feb-07 4:04
Matthew Cuba5-Feb-07 4:04 
AnswerRe: How does this FileSystemWatcher code work? Pin
ShermansLagoon5-Feb-07 3:49
ShermansLagoon5-Feb-07 3:49 
GeneralRe: How does this FileSystemWatcher code work? Pin
Matthew Cuba5-Feb-07 4:04
Matthew Cuba5-Feb-07 4:04 
GeneralRe: How does this FileSystemWatcher code work? Pin
Dan Neely5-Feb-07 4:19
Dan Neely5-Feb-07 4:19 
QuestionRichTextBox Pin
Jeeva Jose5-Feb-07 1:52
Jeeva Jose5-Feb-07 1:52 
AnswerRe: RichTextBox Pin
quiteSmart5-Feb-07 2:03
quiteSmart5-Feb-07 2:03 
QuestionDataGrid Pin
Siva Myneni5-Feb-07 1:01
Siva Myneni5-Feb-07 1:01 
AnswerRe: DataGrid Pin
VirtualVoid.NET6-Feb-07 3:40
VirtualVoid.NET6-Feb-07 3:40 
GeneralRe: DataGrid Pin
Siva Myneni6-Feb-07 19:36
Siva Myneni6-Feb-07 19:36 
GeneralRe: DataGrid Pin
VirtualVoid.NET7-Feb-07 1:44
VirtualVoid.NET7-Feb-07 1:44 
GeneralRe: DataGrid Pin
Siva Myneni7-Feb-07 17:58
Siva Myneni7-Feb-07 17:58 
Questiontable control paging Pin
acodman5-Feb-07 0:53
acodman5-Feb-07 0:53 
AnswerRe: table control paging Pin
Stefan Troschuetz5-Feb-07 1:48
Stefan Troschuetz5-Feb-07 1:48 
Questionopen MenuItem of MainMenu by code Pin
nadapublicidad4-Feb-07 23:55
nadapublicidad4-Feb-07 23:55 
AnswerRe: open MenuItem of MainMenu by code Pin
quiteSmart5-Feb-07 0:06
quiteSmart5-Feb-07 0:06 
GeneralMessage Removed Pin
5-Feb-07 0:25
nadapublicidad5-Feb-07 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.