c# - Is it bad practice to have multiple DoWorkEventHandlers? -
i have gui-application perform different validations. of these validations might take longer running them in backgroundworker, current code looks this:
//sample validator interface public interface validator { void validate(); } //bgws dowork-method: private void mybackgroundworker_dowork(object sender, doworkeventargs e) { validatora.validate(); validatorb.validate(); validatorc.validate(); }
now there validators have support cancelation well. can achieved adding pile of code or can way:
public interface validator { void dovalidationwork(object sender, doworkeventargs e); } class normalvalidator { void dovalidationwork(object sender, doworkeventargs e) { //validation-work } } class cancelablevalidator { void dovalidationwork(object sender, doworkeventargs e) { backgroundworker bgw = sender backgroundworker; while(!bgw.cancellationpending) { //validation-work } } } //setup mybgw.dowork += validatora.dovalidationwork; mybgw.dowork += cancelablevalidatorb.dovalidationwork; mybgw.dowork += validatorc.dovalidationwork;
this yield same result according understanding, every validator can handle cancelation on own.
is okay have multiple dowork-handlers or bad practice?
you may find cleaner use inline anonymous functions majority of these. eg:
backgroundworker worker = new backgroundworker(); worker.dowork += (s, dwe) => { dwe.result = ... // work here }; worker.runworkercompleted += (s, rwe) => { if (rwe.error != null) { // show dialog/message } else { var = rwe.result; } }; worker.runworkerasync();
Comments
Post a Comment