WPF BackgroundWorker vs. Dispatcher

The main difference between the Dispatcher and other threading methods is that the Dispatcher is not actually multi-threaded. The Dispatcher governs the controls, which need a single thread to function properly; the BeginInvoke method of the Dispatcher queues events for later execution (depending on priority etc.), but still on the same thread. BackgroundWorker on the … Read more

Can you link to a good example of using BackgroundWorker without placing it on a form as a component?

This article explains everything you need clearly. Here are the minimum steps in using BackgroundWorker: Instantiate BackgroundWorker and handle the DoWork event. Call RunWorkerAsync, optionally with an object argument. This then sets it in motion. Any argument passed to RunWorkerAsync will be forwarded to DoWork’s event handler, via the event argument’s Argument property. Here’s an … Read more

How to cancel a long-running Database operation?

If you’re using ADO.NET and SQL data provider, take a look at SqlCommand.Cancel method. That does what you’re looking for. However, it tries to cancel and the cancellation may take time. Basically, it’s up to SQL Server to decide when to grant your cancellation request. When the query is cancelled, you should get a SqlException … 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

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

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

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

tech