The final option seems reasonable to me - and you can avoid the duplication of the final two lines, and even the one that you missed (the DoWork
event):
public static BackgroundWorker CreateBackgroundWorker
(DoWorkEventHandler workHandler,
ProgressChangedEventHandler progressHandler,
RunWorkerCompletedEventHandler completedHandler)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += workHandler;
worker.ProgressChanged += progressHandler;
worker.RunWorkerCompleted += completedHandler;
}
Then call that with (say):
BackgroundWorker = BackgroundWorkerHelper.CreateBackgroundWorker(
wkrBackground_DoWork, wkrBackground_ProgressChanged,
wkrBackground_RunWorkerCompleted);
Alternatively, having an interface with a method for each of the DoWork
, ProgressChanged
and RunWorkerCompleted
events would be pretty sweet too, as per Matthew's suggestion... although it makes those three methods effectively public when they can otherwise be private. It might help with testing, mind you...