c# function which has method parameter value, or using delegate -


i have 2 button click events in window form application.

private void butprocess_1_click(...) {     /// below parameters sample.     process_1(int_param1, decimal_param2, datetime_param3); }  private void butprocess_2_click(...) {     /// below parameters sample.     process_2(string_param1, guid_param2, bytearray_param3, bool_param4); } 

as processes take longer time finish executing, need display progress bar user.
modify event called butprocess_1_click.

private void butprocess_1_click(...) {     frmloadingcontrol _frmloadingcontrol = new frmloadingcontrol();                 _frmloadingcontrol.show(this);      backgroundworker _backgroundworker = new backgroundworker();     _backgroundworker.dowork += (s, args) =>     {         this.invoke(new methodinvoker(() => this.enabled = false));         /// below parameters sample.         process_1(int_param1, decimal_param2, datetime_param3);     };     _backgroundworker.runworkercompleted += (s, args) =>     {         _frmloadingcontrol.close();         this.invoke(new methodinvoker(() => this.enabled = true));     };      _backgroundworker.runworkerasync();  } 

everything working correctly.but problem butprocess_2_click need copy code butprocess_1_click. , have change 1 line invoke process_2().

process_2(string_param1, guid_param2, bytearray_param3, bool_param4); 

i don't want duplicate code. want below.

public void genericfunction(function _functioncode) {     frmloadingcontrol _frmloadingcontrol = new frmloadingcontrol();                 _frmloadingcontrol.show(this);      backgroundworker _backgroundworker = new backgroundworker();     _backgroundworker.dowork += (s, args) =>     {         this.invoke(new methodinvoker(() => this.enabled = false));         /// below parameters sample.         //process_1(int_param1, decimal_param2, datetime_param3);         //process_2(string_param1, guid_param2, bytearray_param3, bool_param4);         execute(_functioncode);     };     _backgroundworker.runworkercompleted += (s, args) =>     {         _frmloadingcontrol.close();         this.invoke(new methodinvoker(() => this.enabled = true));     };      _backgroundworker.runworkerasync(); }  private void butprocess_1_click(...) {     /// below parameters sample.     //process_1(int_param1, decimal_param2, datetime_param3);     genericfunction(process_1(int_param1, decimal_param2, datetime_param3)); }  private void butprocess_2_click(...) {     /// below parameters sample.     //process_2(string_param1, guid_param2, bytearray_param3, bool_param4);     genericfunction(process_2(string_param1, guid_param2, bytearray_param3, bool_param4)); } 

please let me suggestion.

pass delegate, this:

// change here public void genericfunction(action action) {     frmloadingcontrol _frmloadingcontrol = new frmloadingcontrol();                 _frmloadingcontrol.show(this);      backgroundworker _backgroundworker = new backgroundworker();     _backgroundworker.dowork += (s, args) =>     {         this.invoke(new methodinvoker(() => this.enabled = false));          // change here         action();          execute(_functioncode);     };     _backgroundworker.runworkercompleted += (s, args) =>     {         _frmloadingcontrol.close();         this.invoke(new methodinvoker(() => this.enabled = true));     };      _backgroundworker.runworkerasync(); }  private void butprocess_1_click(...) {     // change here     genericfunction(() => process_1(int_param1, decimal_param2, datetime_param3)); } 

shouldn't this: _frmloadingcontrol.close(); put in invoke in next row? "acts" on "piece" of winforms...

this.invoke(new methodinvoker(() =>  {     _frmloadingcontrol.close();     this.enabled = true; })); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -