Sending Arguments To Background Worker?

You start it like this: int value = 123; bgw1.RunWorkerAsync(argument: value); // the int will be boxed and then private void worker_DoWork(object sender, DoWorkEventArgs e) { int value = (int) e.Argument; // the ‘argument’ parameter resurfaces here … // and to transport a result back to the main thread double result = 0.1 * value; … Read more

How to stop BackgroundWorker correctly

CancelAsync doesn’t actually abort your thread or anything like that. It sends a message to the worker thread that work should be cancelled via BackgroundWorker.CancellationPending. Your DoWork delegate that is being run in the background must periodically check this property and handle the cancellation itself. The tricky part is that your DoWork delegate is probably … Read more

How to wait for a BackgroundWorker to cancel?

If I understand your requirement right, you could do something like this (code not tested, but shows the general idea): private BackgroundWorker worker = new BackgroundWorker(); private AutoResetEvent _resetEvent = new AutoResetEvent(false); public Form1() { InitializeComponent(); worker.DoWork += worker_DoWork; } public void Cancel() { worker.CancelAsync(); _resetEvent.WaitOne(); // will block until _resetEvent.Set() call made } void … Read more

BackgroundWorker RunWorkerCompleted Event

If the BackgroundWorker was created from the UI thread, then the RunWorkerCompleted event will also be raised on the UI thread. If it was created from a background thread, the event will be raised on an undefined background thread (not necessarily the same thread, unless you’re using a custom SynchronizationContext). Interestingly, this doesn’t seem to … Read more

How to correctly implement a BackgroundWorker with ProgressBar updates?

As you haven’t shown your full BackgroundWorker code, I can’t tell if you have implemented it correctly. As such, all I can do is to show you a simple working example of updating a ProgressBar control: UserControl XAML: <UserControl x:Class=”WpfApplication1.Views.TestView” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ mc:Ignorable=”d” d:DesignHeight=”300″ d:DesignWidth=”300″ Loaded=”UserControl_Loaded”> <ProgressBar x:Name=”progressBar” Height=”25″ Margin=”20″ Minimum=”0″ Maximum=”50″ /> … 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

How to use WPF Background Worker

Add using using System.ComponentModel; Declare Background Worker: private readonly BackgroundWorker worker = new BackgroundWorker(); Subscribe to events: worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted; Implement two methods: private void worker_DoWork(object sender, DoWorkEventArgs e) { // run all background tasks here } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //update ui once worker complete his work } … Read more

How to use a BackgroundWorker?

You can update progress bar only from ProgressChanged or RunWorkerCompleted event handlers as these are synchronized with the UI thread. The basic idea is. Thread.Sleep just simulates some work here. Replace it with your real routing call. public Form1() { InitializeComponent(); backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; backgroundWorker1.WorkerReportsProgress = true; } private void button1_Click(object sender, … Read more