Delegate Dies

J

John

I'm using the FileSystemWatcher class to get notified when a file is
created:

myFileSystemWatcher.Created += new FileSystemEventHandler(myHandler);

Sometimes (though rarely), myHandler executes fine. However, most of the
time it appears to die on the first line of execution:

private void myHandler(object source, FileSystemEventArgs e)
{
try
{
Thread.Sleep(4000);
// Do more stuff
}
catch(Exception aException)
{
// This never get called
}
}

If I set a breakpoint on the line:
Thread.Sleep(4000);

It takes a long time for VS2005 to break. However, when it does, everything
looks fine (as far as parameters, registers, etc.). I don't know if I'm
overflowing the stack somehow, or why it is dying.

Does anybody have any ideas? Thanks,
-- John
 
R

RobertW

Umm.. What's creating the file(s)?

I didn't look it up in the documentation, but from what you wrote, I gather
this function will be called whenever a file (or any file?) is created (i.e.
not updated or modified, just created from a deleted or non-existent file).

I'm going to guess your file(s) aren't being created too often, and only
when a file is created is your event handler being reached. i.e. the event
handler isn't the problem.

But honestly, I don't think you gave enough sample code or information to
know that one way or the other.

-Rob
 
J

John

Hey Rob,

All of your assumptions are correct. I don't think the event handler is the
problem either. I'm actually completely uncertain where to even begin to
look though. I have no idea why the code would just all of the sudden die
in that thread. I didn't write all of the code for this project, I'm just
the guy that has to maintain it.

Any other random ideas why a thread would all of the sudden just die?
 
R

RobertW

Ok, Random thoughts I'm good with... Useful ? Eh..

From what I understand about threads, and I don't do a whole lot of
multithreaded programming (the work I do wouldn't benefit that much from it,
though I do it in some areas like background loading of data):

-Event handling should happen in the main thread, and when the main thread
exits, the program exits.
-If you want a sub-thread or new-thread to handle an event, you catch the
event in the main thread, then in the event handler, call a 'real event
handler' in a new thread (i.e. create a new thread for that purpose), and
return immediately so the flow of control returns normally to the main thread.

So, when you say 'the thread dies' I'm not sure if you mean a sub-thread
dies or the main program thread dies. If the whole program dies, then that's
a bigger problem you may need to troubleshoot with a debugging tool to find
what causes the main program to exit.

This is not based on specific knowledge of threading in .net, but rather
older threading models I've used.

-Rob
 
J

John

Hmmm....

This appeared to only happen when I had a breakpoint set. If I uncheck all
of my breakpoints, everything works fine. For now, I'm going to blame this
on some exception checking in VS2005. Only because I have no other idea at
the moment how to track this down if a problem does exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top