Method being triggered n! (n factorial) times instead of n times

I

Ian Toltz

I've got this bizarre problem with a program I'm making...

I've got a kludgy mickey mouse solution for communicating with a
server... The program writes to a file, and a script on the server
parses that file then creates a response file with a randomly-
generated filename which a FileSystemWatcher in the original program
looks for. When it sees it, it reads the response and then deletes the
file.

Here's my problem... It seemed to work at first, but I just went and
was trying something that involved communicating with the server more
than once, and got a file not found exception the second time... After
some trouble shooting, I've discovered that the second time, it
triggers the response method twice. The third time, it triggers the
response three times. Etc.

Here's the code that I think is relevant:

FileSystemWatcher fsw = new FileSystemWatcher(responseDir);

private void watchForResponse()
{
fsw.Filter = "*" + token + "*";
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = false;

fsw.Renamed += new RenamedEventHandler(gotResponse);
}

private void gotResponse(Object source, RenamedEventArgs rea)
{
fsw.EnableRaisingEvents = false;
//Sleep to avoid stepping on toes with file operations
Thread.Sleep(500);
StreamReader SR;
String S = "";
String filename = responseDir + "\\" + token;
try
{
SR = File.OpenText(filename);
String temp = SR.ReadLine();
while (temp != null)
{
S += temp;
temp = SR.ReadLine();
}
SR.Close();
...does stuff to S string...
File.Delete(filename);
}
catch (Exception e)
{
MessageBox.Show(e.Message, filename);
}
}
 
B

Ben Voigt

Here's my problem... It seemed to work at first, but I just went and
was trying something that involved communicating with the server more
than once, and got a file not found exception the second time... After
some trouble shooting, I've discovered that the second time, it
triggers the response method twice. The third time, it triggers the
response three times. Etc.

Here's the code that I think is relevant:

gotResponse sets EnableRaisingEvents = false, you must set it true again
somewhere. Your snippets don't show it, but I suspect you are calling
watchForResponse from gotResponse, which not only re-enables events, but
adds a second (and third, and Nth) instance of gotResponse to the Renamed
event. EventHandlers aren't oneshot, they persist.
 
I

Ian Toltz

gotResponse sets EnableRaisingEvents = false, you must set it true again
somewhere. Your snippets don't show it, but I suspect you are calling
watchForResponse from gotResponse, which not only re-enables events, but
adds a second (and third, and Nth) instance of gotResponse to the Renamed
event. EventHandlers aren't oneshot, they persist.

Ah, that makes perfect sense! So all I need to do is set the event
handler outside of the function (probably where I define the file
system watcher) and that should all clear up.

Thanks!
 

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