Skip to content

Instantly share code, notes, and snippets.

@Larry57
Last active December 13, 2015 21:39
Show Gist options
  • Select an option

  • Save Larry57/4979189 to your computer and use it in GitHub Desktop.

Select an option

Save Larry57/4979189 to your computer and use it in GitHub Desktop.
Special class to handle Events Mask nicely
// 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