RunWorkerAsync() method of BackgroundWorker doesn't trigger Do_Work event

1k Views Asked by At

I have used a backgroundworker to set a ProgressBar to show the progress when downloading a file. But the RunWorkerAsync() method of BackgroundWorker doesn't trigger Do_Work event.

/**********Window_Load***********/

        BackgroundWorker backgroundWorker1;
    private void MainWindow_Load(object sender, RoutedEventArgs e)
    {
    backgroundWorker1=new BackgroundWorker();
    backgroundWorker1.WorkerReportsProgress=true;
    backgroundWorker1.DoWork +=new DoWorkEventHandler(backgroundWorker1_DoWork);
    backgroundWorker1.ProgressChanged +=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    backgroundWorker1.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
    }

/******Button_Click*******/

void Button_Click(object sender, RoutedEventArgs e)
    {
            backgroundWorker1.RunWorkerAsync();
    }

/****Events*********/

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressBar1.Value = e.ProgressPercentage;
    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("File download complete");
    }


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    //Code to download a file and setting progressbar time. 
    }

I am new to WPF. Any help appreciated.

3

There are 3 best solutions below

3
On

Your code looks fine there is no change in code..try to figure out following things

  1. Check your click event is working ...button click is working
  2. put break point in click event and debug it if click event is working

I suggest you can look this article once : Multi-threading with the BackgroundWorker which is doing same thing as you are doing.. Might be your find something missing.

0
On

I setup a sample project and used your code exactly, and there's nothing programmatically wrong with it and it works perfectly on my side.

make sure you subscribed your button to the click event, which seems to not be being fired

<Button Content="Button" HorizontalAlignment="Left" Margin="153,42,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
1
On

Try this example ,

<Button Content="Button" HorizontalAlignment="Left" Margin="165,58,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

 private BackgroundWorker _backgroundWorker;
        public MainWindow()
        {
            InitializeComponent();
            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            _backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            _backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
            _backgroundWorker.WorkerReportsProgress = true;
            DataContext = this;


        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var eee = e.Result;
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            var progrss = e.ProgressPercentage;
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int p=10;
            for (int index = 0; index < p; index++)
            {
                object param = "something";
                Thread.Sleep(100);
                _backgroundWorker.ReportProgress(p, param);
                Thread.Sleep(1000);
                if (index == 10)
                {
                    break;
                } 
            } 

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _backgroundWorker.RunWorkerAsync(); 
        }