Events are running twice

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been designing an application in Visual Studio 2005, and just about
everything is working, except all of my events run twice. They run through
correctly, but then they randomly run through a second time. For example, if
I click cancel on the main form, it properly triggers the event, but it does
so twice. The main program is program.cs, and it calls the others. Here is
the source code, its too long to list here, so here are some links:

http://www.joshkelahan.com/slu/AutoLogout/Program.cs.pdf
http://www.joshkelahan.com/slu/AutoLogout/frm_main.cs.pdf
http://www.joshkelahan.com/slu/AutoLogout/frm_cncl.cs.pdf
http://www.joshkelahan.com/slu/AutoLogout/autologout_about.cs.pdf

And here are the Designer files created by Visual Studio
http://www.joshkelahan.com/slu/AutoLogout/frm_cncl.Designer.cs.pdf
http://www.joshkelahan.com/slu/AutoLogout/frm_main.Designer.cs.pdf
http://www.joshkelahan.com/slu/AutoLogout/autologout_about.Designer.cs.pdf

Any help you can provide on how I can get these events to trigger only once
would be greatly appreciated.

Thanks!
 
You're hooking the events up twice. :)

Do a quick search for "+=" on the event, put a breakpoint there, on see how
many times it gets called. I would be willing to bet, it's exactly two
times.

If you add an event handler, you need to remembe to remove it. Otherwise,
you can add it multiple times, and then it's called multiple times.
 
You know, i have never seen that -= operator in any example either in the
MSDN documentation or in the C# books that I have. I guess it makes sense
though. It fixed my problem, Thanks!

-Josh
 
jdkc4d said:
You know, i have never seen that -= operator in any example either in the
MSDN documentation or in the C# books that I have. I guess it makes sense
though. It fixed my problem, Thanks!

-Josh

Would you mind posting an example of code that was causing this... i
wouldn't mind just seeing what the cause was.

Was it something like "handles += onclick or something ?

M.
 
Miro said:
Would you mind posting an example of code that was causing this... i
wouldn't mind just seeing what the cause was.

Was it something like "handles += onclick or something ?

If your code looked like:
Button1.OnClick += MyButtonHandler(...);
Button1.OnClick += MyButtonHandler(...);

When the user clicks button1 a single time, your method ("MyButtonHandler")
will actually be called twice.

If you're adding event handlers based on state, then you need to remember to
remove them as well:
Button1.Onclick -= MyButtonHandler(...);
 
Chris said:
If your code looked like:
Button1.OnClick += MyButtonHandler(...);
Button1.OnClick += MyButtonHandler(...);

When the user clicks button1 a single time, your method ("MyButtonHandler")
will actually be called twice.

If you're adding event handlers based on state, then you need to remember to
remove them as well:
Button1.Onclick -= MyButtonHandler(...);
Thank you

Miro
 
Back
Top