473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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

Mar 19 '07 #1
2 1176
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.
>
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);
}
}

Mar 19 '07 #2
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!

Mar 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marius Rus | last post by:
I have a project in C# that do the followings: From an csv text file it is taking datas and inserted into an paradox database. My problem it is that when writing into the paradox database it is...
1
by: thomaz | last post by:
Hi; My code below doesn´t work well in windows 98 only. I use them in windows XP and it´s OK. thisBuilder = new OleDbCommandBuilder (thisAdapter); thisDataSet = new DataSet ();
2
by: Cris Curtis | last post by:
When I use an embed tag that uses a dynamic aspx page, the dynamic aspx page appears to get called 2 times instead. Below is code that adds an embed tag to a placeholder control that will use...
2
by: Torben Philippsen | last post by:
Hi, I have a very simple searchform (an asp:textbox and a asp:button) I have enabled autopostback for the textbox to be able to execute the search method when the user hits the enter key. ...
1
by: Martin | last post by:
I wonder what is happening if you subsequently use the session.add method with the same key, but different values. e.g. if I do; session.add("MyVar", "a") session.add("MyVar", "b") then;...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
4
by: viuxrluxvbbc | last post by:
Hi im trying to write a program that will read in numbers and display them in ascending order along with a count of how many times it repeats. i got the numerical order portion done but cant figure...
4
by: kwatch | last post by:
Hi, I have a question about os.times(). os.times() returns a tuple containing user time and system time, but it is not matched to the result of 'time' command. For example, os.times() reports...
10
by: Steven W. Orr | last post by:
In the program below, I want this instance to end up calling repmeth whenever inst.m1 is called. As it is now, I get this error: Hello from init inst = <__main__.CC instance at 0x402105ec>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.