How to make BackgroundWorker return an object

In your DoWork event handler for the BackgroundWorker (which is where the background work takes place) there is an argument DoWorkEventArgs. This object has a public property object Result. When your worker has generated its result (in your case, a List<FileInfo>), set e.Result to that, and return. Now that your BackgroundWorker has completed its task, … Read more

Proper way to Dispose of a BackGroundWorker

BackgroundWorker derives from Component. Component implements the IDisposable interface. That in turn makes BackgroundWorker inherit the Dispose() method. Deriving from Component is a convenience for Windows Forms programmers, they can drop a BGW from the toolbox onto a form. Components in general are somewhat likely to have something to dispose. The Windows Forms designer takes … Read more

Running a method in BackGroundWorker and Showing ProgressBar

Instead of using one ParseFiles method (which should depend on myBGWorker) use loop and method which parse one file. Report progress percentage in that loop: private void parseButton_Click(object sender, EventArgs e) { parseButton.Enabled = false; myBGWorker.RunWorkerAsync(); } private void myBGWorker_DoWork(object sender, DoWorkEventArgs e) { for(int i = 0; i < filesCount; i++) { ParseSingleFile(); // … Read more

Unhandled exceptions in BackgroundWorker

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event … Read more

How can I make a background worker thread set to Single Thread Apartment?

This is not possible, BGW uses a threadpool thread. TP threads are always MTA, it cannot be changed. You will have to use a regular Thread, call SetApartmentState() before you start it. This thread also should pump a message loop, call Application.Run(). Maybe you ought to consider calling this code from the UI thread. Because … Read more

Unhandled exceptions in BackgroundWorker

What you’re describing is not the defined behavior of BackgroundWorker. You’re doing something wrong, I suspect. Here’s a little sample that proves BackgroundWorker eats exceptions in DoWork, and makes them available to you in RunWorkerCompleted: var worker = new BackgroundWorker(); worker.DoWork += (sender, e) => { throw new InvalidOperationException(“oh shiznit!”); }; worker.RunWorkerCompleted += (sender, e) … Read more

How to “kill” background worker completely?

You can use something like this (for more information about aborting managed threads and about ThreadAbortException see “Plumbing the Depths of the ThreadAbortException Using Rotor (Web archive)” by Chris Sells): public class AbortableBackgroundWorker : BackgroundWorker { private Thread workerThread; protected override void OnDoWork(DoWorkEventArgs e) { workerThread = Thread.CurrentThread; try { base.OnDoWork(e); } catch (ThreadAbortException) { … Read more