How to wait correctly until BackgroundWorker completes?

Try using the AutoResetEvent class like this: var doneEvent = new AutoResetEvent(false); var bw = new BackgroundWorker(); bw.DoWork += (sender, e) => { try { if (!e.Cancel) { // Do work } } finally { doneEvent.Set(); } }; bw.RunWorkerAsync(); doneEvent.WaitOne(); Caution: You should make sure that doneEvent.Set() is called no matter what happens. Also you … Read more

How to update GUI with backgroundworker?

You need to declare and configure the BackgroundWorker once – then Invoke the RunWorkerAsync method within your loop… public class UpdateController { private UserController _userController; private BackgroundWorker _backgroundWorker; public UpdateController(LoginController loginController, UserController userController) { _userController = userController; loginController.LoginEvent += Update; _backgroundWorker = new BackgroundWorker(); _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork); _backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); _backgroundWorker.WorkerReportsProgress= true; } … 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

Spawn Multiple Threads for work then wait until all finished

My preference for this is to handle this via a single WaitHandle, and use Interlocked to avoid locking on a counter: class Program { static void Main(string[] args) { int numThreads = 10; ManualResetEvent resetEvent = new ManualResetEvent(false); int toProcess = numThreads; // Start workers. for (int i = 0; i < numThreads; i++) { … Read more

This BackgroundWorker is currently busy and cannot run multiple tasks concurrently

Simple: Don’t start the BackgroundWorker twice. You can check if it is already running by using the IsBusy property, so just change this code: worker.RunWorkerAsync(); to this: if( !worker.IsBusy ) worker.RunWorkerAsync(); else MessageBox.Show(“Can’t run the worker twice!”); Update: If you do actually need to launch multiple background tasks at the same time, you can simply … 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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)