Last active
December 13, 2015 21:39
-
-
Save Larry57/4979189 to your computer and use it in GitHub Desktop.
Special class to handle Events Mask nicely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Usage : | |
| textBoxChangedEventMask = new EventMask( | |
| () => { textBox1.TextChanged += new EventHandler(textBox1_TextChanged); }, | |
| () => { textBox1.TextChanged -= new EventHandler(textBox1_TextChanged); } | |
| ); | |
| //in the event code: | |
| try | |
| { | |
| textBoxChangedEventMask.Push(); | |
| // ... | |
| } | |
| finally | |
| { | |
| textBoxChangedEventMask.Pop(); | |
| } | |
| public class EventMask | |
| { | |
| Action hook; | |
| Action unHook; | |
| int count = 0; | |
| public EventMask(Action hook, Action unHook) | |
| { | |
| this.hook = hook; | |
| this.unHook = unHook; | |
| } | |
| public void Push() | |
| { | |
| count++; | |
| if (count == 1) | |
| unHook(); | |
| } | |
| public void Pop() | |
| { | |
| count--; | |
| if (count == 0) | |
| hook(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment