c# - Enabling/Disabling the menu item from another thread -
i trying change menu item thread. able use invokerequired/invoke on other controls, since menu item not control, having difficulty in achieving same functionality.
for other controls, doing this:
private delegate void setcontrolenablehandler(object sender, boolean bvalue); private void setcontrolenabled(object sender, boolean bvalue) { control control = (control)sender; if (control.invokerequired) control.invoke( new setcontrolenablehandler(setcontrolenabled), new object[] { sender, bvalue } ); else control.enabled = bvalue; }
from worker thread simple call:
this.setcontrolenabled(btnpress, true);
and job.
can me menu item here?
thank you, -bhaskar
the menu item not control, form hosting menustrip is. so, method in form can modify menuitem, if called in correct thread.
so,
private void enablemenuitem(toolstripmenuitem item, bool enabled) { this.begininvoke(new methodinvoker(delegate() { item.enabled = enabled; } )); }
will want. note use of anonymous method save have define delegate (probably) isn't going used elsewhere.
also, aside, overload of control.invoke you're using has it's second argument marked params [] - how c# inplements variable numbers of arguments. don't have contstruct object array, add many objects need parameters.
for example,
control.invoke(new setcontrolenablehandler(setcontrolenabled), new object[] { sender, bvalue } );
can written
control.invoke( new setcontrolenablehandler(setcontrolenabled), sender, bvalue);
it's nicer, i'm sure you'll agree.
Comments
Post a Comment