Tracking Multiple BackgroundWorkers Tracking Multiple BackgroundWorkers wpf wpf

Tracking Multiple BackgroundWorkers


From what I understand, you don't want to actually abort the thread, you just want it to continue working silently (i.e. not update the UI)? One way would be to keep a list of BackgroundWorkers and remove their event handlers if they're to be "aborted".

List<BackgroundWorker> allBGWorkers = new List<BackgroundWorker>();//user creates a new bg worker.BackgroundWorker newBGWorker = new BackgroundWorker();//.... fill out properties//before adding the new bg worker to the list, iterate through the list //and ensure that the event handlers are removed from the existing ones    foreach(var bg in allBGWorkers){       bg.ProgressChanged -= backgroundWorker_ProgressChanged;   bg.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;}//add the latest bg worker you createdallBGWorkers.Add(newBGWorker);

That way you can keep track of all the workers. Since a List maintains order, you'll know which one the latest on is (the last one in the list) but you can just as easily use a Stack here if you prefer.