Alexandros Mor
Alexandros Mor

Reputation: 127

Event is firing multiple times but the code should execute only once

I have an event that fires 6 times and I want my pop up appears only once and not 6 times. I tried to use a flag but I can not reset it from the same event. I tried to compare values but didn't work as I have the same result all the times. Any ideas ?

Upvotes: 1

Views: 1667

Answers (2)

L-Four
L-Four

Reputation: 13531

I assume this is in a winforms app that is single-threaded: you could use a static boolean variable 'EventReceived' that you set to true in the first event after showing the popup; as long as 'EventReceived' is true don't show the popup again; and set 'EventReceived' back to false if the popup is closed.

Of course, the question is, why do you receive the event multiple times. Maybe you can avoid this or deal with it differently - but then you have to give us more information.

Upvotes: 2

ANjaNA
ANjaNA

Reputation: 1426

Try with a static flag variable. Then you can reset the flag from the same event. At the first time application execute the onEvent code segment you need to mark the flag. In the inEvent code segment you can include your operation and before reach to that part of code you must check your flag. (via if clause). If you need to reset your flag after 6 times you can maintain a counter variable and by the counter value you can calculate in which firing cycle you are in. When the counter reach to 6 you can reset your flag variable.

Upvotes: 1

Related Questions