I have an assembly (.NET class library) that contains an object that implements EAP for asynchronous methods:
class Product
{
void Install();
void InstallAsync();
EventHandler<InstallProgressChangedEventArgs> Installing;
EventHandler<InstallCompletedEventArgs> Installed;
}
I am contemplating whether to use “standard” Thread or use the BackgroundWorker class provided by .NET.
I’ve read that one should only use BackgroundWorker when it is expected that the object will be used in “Windows Forms” or UI. The class library I’m writing can be used on Windows Form, WPF Application and Console application.
Also, if I am to use the BackgroundWorker class, is it “proper” to not subclass from the BackgroundWorker class? I don’t want to expose the class’ members as I will be providing my own events (Installing and Installed).
If the BackgroundWorker class is not appropriate, what can you recommend and why?
There’s no problems using the BW component outside of Windows forms UI applications. It’s meant to be used where you just need to easily fire off a background thread and receive the callback when that work is complete.
If your only requirement is that you want to start up a thread to do work, and get results back, and you don’t need the control you get by managing the thread yourself, I would recommend you use it.
I wouldn’t subclass it though, unless you want your component to BE a background worker itself. That is, you’re extending its behaviors. If you’re simply using its behaviors there’s no need to subclass, just use composition.