Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background worker runs doWork multiple times

private void button_uploadToPI_Click(object sender, RoutedEventArgs e)
    {


        this.newFilePath = this.textbox_input_filePath.Text;
        this.label_status.Content = "";
        if (bgWorker.IsBusy != true)
        {


            bgWorker.RunWorkerAsync();
        }
    }

Here is my Click Event.

public MainWindow()
    {

        InitializeComponent();

        this.progressBar.Minimum = 0;
        this.progressBar.Maximum = 100;

        this.bgWorker = new BackgroundWorker();

        this.bgWorker.WorkerReportsProgress = true;
        this.bgWorker.WorkerSupportsCancellation = true;
        reader.IsOpen = false;

        this.bgWorker.DoWork += bgWorker_DoWork;
        this.bgWorker.ProgressChanged += bgWorker_ProgressChanged;
        this.bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
    }

Main window method.

public partial class MainWindow : Window
{

    private BackgroundWorker bgWorker;

bgWorker is initialized here.

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        this.progressBar.Value = 0;
        this.label_progressPercentage.Content = "0%";
        reportTextBox.ScrollToEnd();
    }

RunWorkComplete code

The main issue is, if i run the application once, it works as expected. If i then click again, it will execute the method twice, and if i was to click it again it would execute the method 3 times. The will increment every new click.

Can anyone see the main issue here?

like image 740
Andrew Avatar asked Oct 27 '25 08:10

Andrew


1 Answers

A sample I worked on against @galenus's suggestion. I had the same situation with background worker, clicking the do work button firing the dowork multiple times until I initialized the worker with the button. Code block:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Data;

namespace MouseClick
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {



        public MainWindow()
        {
            InitializeComponent();
        }

        public BackgroundWorker worker = new BackgroundWorker();

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //e.Handled = true; // Din't do anything 
            worker = new BackgroundWorker(); //stopped the dowork being executed multiple times when button pressed again in the same session
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += worker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();

        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {


            int tot = 1;

            MessageBox.Show(tot.ToString()); //if this message box appears multiple times, it means the dowork is executing multiple times

            DataTable dt = new DataTable();

            dt.Columns.Add("Running Number");

            for (int i = 0; i <= 100; i++)
            {
                if (worker.CancellationPending == true)
                {
                    //http://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result
                    //  e.Cancel = true; //This does the trick
                    e.Result = 100;
                    return;
                }
                worker.ReportProgress(i);
                System.Threading.Thread.Sleep(1);
                dt.Rows.Add(i);
            }
            e.Result = dt;
        }

        #region "worker_ProgressChanged"
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }
        #endregion

        #region "worker_RunWorkerCompleted"
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            myGrid.ItemsSource = ((DataTable)e.Result).DefaultView;
            worker.Dispose();

        }
        #endregion

    }
}
like image 92
Rajesh Thampi Avatar answered Oct 29 '25 23:10

Rajesh Thampi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!