Events are running twice

Events are running twice

am 04.10.2007 21:25:02 von jdkc4d

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.c s.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.D esigner.cs.pdf

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

Thanks!

Re: Events are running twice

am 04.10.2007 22:22:02 von Chris Mullins

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.

--
Chris Mullins

"jdkc4d" wrote in message
news:F069BA0C-8739-448F-A2CE-CB63C1D8EB65@microsoft.com...
>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.c s.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.D esigner.cs.pdf
>
> Any help you can provide on how I can get these events to trigger only
> once
> would be greatly appreciated.
>
> Thanks!
>
>
>

Re: Events are running twice

am 05.10.2007 15:48:00 von jdkc4d

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

"Chris Mullins [MVP - C#]" wrote:

> 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.
>
> --
> Chris Mullins
>
> "jdkc4d" wrote in message
> news:F069BA0C-8739-448F-A2CE-CB63C1D8EB65@microsoft.com...
> >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.c s.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.D esigner.cs.pdf
> >
> > Any help you can provide on how I can get these events to trigger only
> > once
> > would be greatly appreciated.
> >
> > Thanks!
> >
> >
> >
>
>
>

Re: Events are running twice

am 05.10.2007 16:28:53 von miro

jdkc4d wrote:
> 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
>
> "Chris Mullins [MVP - C#]" wrote:
>
>> 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.
>>
>> --
>> Chris Mullins
>>
>> "jdkc4d" wrote in message
>> news:F069BA0C-8739-448F-A2CE-CB63C1D8EB65@microsoft.com...
>>> 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.c s.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.D esigner.cs.pdf
>>>
>>> Any help you can provide on how I can get these events to trigger only
>>> once
>>> would be greatly appreciated.
>>>
>>> Thanks!
>>>
>>>
>>>
>>
>>

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.

Re: Events are running twice

am 05.10.2007 18:29:00 von Chris Mullins

"Miro" wrote:

> 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 Mullins

Re: Events are running twice

am 05.10.2007 20:42:55 von miro

Chris Mullins [MVP - C#] wrote:
> "Miro" wrote:
>
>> 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 Mullins
>
>
Thank you

Miro