c# Disable event for some time -
i have set of functions unregister event listbox1_selectedvaluechanged amount of time.
the function called: pause().
is there way function, can same variety of event like:
pause(listbox1.selectedvaluechanged)
or
pause(button1.click)
etc.?
private system.windows.forms.timer disableevent = new system.windows.forms.timer(); private void pause(int fortime = 200) { listbox1.selectedvaluechanged -= new eventhandler(listbox1_selectedvaluechanged); disableevent.tick += new eventhandler(disableevent_tick); disableevent.interval = (fortime); disableevent.enabled = true; disableevent.start(); } private void disableevent_tick(object sender, eventargs e) { if (disableevent.enabled == true) { disableevent.tick -= new eventhandler(disableevent_tick); disableevent.stop(); disableevent.enabled = false; listbox1.selectedvaluechanged += new eventhandler(listbox1_selectedvaluechanged); } }
i use datetime
field that. check in selectedvaluedchanged()
event if allowed run. (do not unreguster event)
for example: (pseudo)
public class class1 { private datetime _isenabledafter = datetime.minvalue; public class() { listbox1.selectedvaluechanged += new eventhandler(listbox1_selectedvaluechanged); } public void pause(int timems) { // set _isenabledafter in future. _isenabledafter = datetime.now.addmilliseconds(timems); } public void listbox1_selectedvaluechanged(object sender, eventargs e) { // after _isenabledafter? if(datetime.now < _isenabledafter) // nope... nothing. return; // thing. } }
it save timer , complexity.
could this:
public class class1 { private timeenabledevent _selectedvaluechanged = new timeenabledevent(); public class1() { listbox1.selectedvaluechanged += (s, e) => { if (_selectedvaluechanged.isenabled) listbox1_selectedvaluechanged(s, e); }; _selectedvaluechanged.pause(200); } public void listbox1_selectedvaluechanged(object sender, eventargs e) { // thing. } }
public class timeenabledevent { private datetime _isenabledafter = datetime.minvalue; public void pause(int timems) { // set _isenabledafter in future. _isenabledafter = datetime.now.addmilliseconds(timems); } public bool isenabled { { return (datetime.now >= _isenabledafter); } } }
update 2:
public partial class form1 : form { private timeenabledevent _event = new timeenabledevent(); public form1() { initializecomponent(); listbox1.selectedvaluechanged += _event.check(listbox1_selectedvaluechanged); _event.pause(1000); } private void listbox1_selectedvaluechanged(object sender, eventargs e) { // thing } }
internal class timeenabledevent { internal eventhandler check(eventhandler listbox1_selectedvaluechanged) { return new eventhandler((ss, ee) => { if (datetime.now >= _isenabledafter) listbox1_selectedvaluechanged(ss, ee); }); } private datetime _isenabledafter = datetime.minvalue; public void pause(int timems) { // set _isenabledafter in future. _isenabledafter = datetime.now.addmilliseconds(timems); } }
Comments
Post a Comment