I'm trying to optimize the following workflow:
myObj.SupressEvents = true;
DoSomeWork();
DoSomeMoreWork();
...
myObj.SupressEvents = false;
The problem with that is obvious, either myObj.SupressEvents = true/false; can be missed in between and lead unwanted errors or simply a by use a return can make that reset to false never to be reached, i'm looking for a pattern that avoid forgeting it, something like:
// This is a keyword example, not a function call nor definition
SetAndReset(myObj.SupressEvents, true, false)
{
DoSomeWork();
DoSomeMoreWork();
...
}
I guess this can be done with a delegate or a function with Func, but can it work even if i do a return in between it would reset the variable to false anyway? There are something like that into native C# keywords?