c# - Temporarily stop form events from either being raised or being handled? -


i have ton on controls on form, , there specific time when want stop of events being handled time being. if don't want events handled:

private bool myoprunning = false;  private void opfunction() {     myoprunning = true;     // stuff     myoprunning = false; }  private void somehandler(object sender, eventargs e) {     if (myoprunning) return;     // otherwise, things } 

but have lot of handlers need update. curious if .net has quicker way having update each handler method.

you have create own mechanism this. it's not bad though. consider adding layer of abstraction. example, simple class called filteredeventhandler checks state of myoprunning , either calls real event handler, or suppresses event. class this:

public sealed class filteredeventhandler {     private readonly func<bool> supressevent;     private readonly eventhandler realevent;      public filteredeventhandler(func<bool> supressevent, eventhandler eventtoraise)     {         this.supressevent = supressevent;         this.realevent = eventtoraise;     }      //checks "supress" flag , either call real event handler, or skip     public void fakeeventhandler(object sender, eventargs e)     {         if (!this.supressevent())         {             this.realevent(sender, e);         }     } } 

then when hook event, this:

this.control.whateverevent += new filteredeventhandler(() => myoprunning, realeventhandler).fakeeventhandler; 

when whateverevent gets raised, call filteredeventhandler.fakeeventhandler method. method check flag , either call, or not call real event handler. pretty logically same you're doing, code checks myoprunning flag in 1 place instead of sprinkled on code.

edit answer question in comments:

now, example bit incomplete. it's little difficult unsubscribe event because lose reference filteredeventhandler that's hooked up. example, can't do:

this.control.whateverevent += new filteredeventhandler(() => myoprunning, realeventhandler).fakeeventhandler; //some other stuff. . . this.control.whateverevent -= new filteredeventhandler(() => myoprunning, realeventhandler).fakeeventhandler; //not gonna work! 

because you're hooking 1 delegate , unhooking different one! granted, both delegates fakeeventhandler method, that's instance method , belong 2 different filteredeventhandler objects.

somehow, need reference first filteredeventhandler constructed in order unhook. work, involves keeping track of bunch of filteredeventhandler objects no better original problem you're trying solve:

filteredeventhandler filter1 = new filteredeventhandler(() => myoprunning, realeventhandler); this.control.whateverevent += filter1.fakeeventhandler; //code other stuff. . . this.control.whateverevent -= filter1.fakeeventhandler; 

what do, in case, have filteredeventhandler.fakeeventhandler method pass 'this' reference realeventhandler. involves changing signature of realeventhandler either take parameter:

public void realeventhandler(object sender, eventargs e, filteredeventhandler filter); 

or changing take eventargs subclass create holds reference filteredeventhandler. better way it

public void realeventhandler(object sender, filteredeventargs e); //also change signature of filteredeventhandler constructor: public filteredeventhandler(func<bool> supressevent, eventhandler<filteredeventargs> eventtoraise) {   //. . . } //finally, change fakeeventhandler method call real event , pass reference this.realevent(sender, new filteredeventargs(e, this)); //pass original event args + reference specific filteredeventhandler 

now realeventhandler gets called can unsubscribe because has reference correct filteredeventhandler object got passed in parameters.

my final advice, though not of this! neolisk nailed in comments. doing complicated sign there's problem design. difficult needs maintain code in future (even you, suprisingly!) figure out non-standard plumbing involved.

usually when you're subscribing events, once , forget - in gui program.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -