This is a really simple refactoring problem if all your operations can be represented as one type of delegate. eg:.
private void RunOperationWithMainProgressFeedback( Func<bool> operation, string startMessage, string completionMessage, string cancellationMessage, string errorMessage) { this._UpdateMainForm.BeginInvoke(startMessage, false, null, null); try { if (operation.Invoke()) { this._UpdateMainForm.BeginInvoke(completionMessage, true, null, null); } else { this._UpdateMainForm.BeginInvoke(cancellationMessage, true, null, null); } } catch (Exception) { this._UpdateMainForm.BeginInvoke(errorMessage, true, null, null); throw; } } private void btnX_Click(object sender, EventArgs e) { this.RunOperationWithMainProgressFeedback( this.UpdateOperationA, "StartA", "CompletedA", "CanceledA", "ErrorA"); }
Although you can use a dictionary to store argument values โโ(as suggested in a previous VinayC answer), this is optional. Personally, I would avoid this for reasons of readability as well as performance, but ymmv ...
source share