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);
}
}
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);
}
}