Ian W
Ian W

Reputation: 411

How to correctly implement a BackgroundWorker with ProgressBar updates?

-Updated--14/10 also asked this question

To give some clear idea of what is going on and taking into account the comments and from this article here

What I really want to do now is invoke a new form with a progress bar on it and have that run and animate whilst my back ground thread runs my long process to the database and then invoke a close form event

The background worker is set up here

 public partial class MainWindow : Window
{
    //Declare background workers
    BackgroundWorker bw = new BackgroundWorker();
    BackgroundWorker bwLoadCSV = new BackgroundWorker();
    BackgroundWorker bwProgressBar = new BackgroundWorker();

Then delegates added here

  public MainWindow()
    {
        bwLoadCSV.WorkerReportsProgress = true;
        bwLoadCSV.WorkerSupportsCancellation = true;
        bwLoadCSV.DoWork += new DoWorkEventHandler(bwLoadCSV_DoWork);
        bwLoadCSV.ProgressChanged += new ProgressChangedEventHandler(bwLoadCSV_ProgressChanged);
        bwLoadCSV.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoadCSV_RunWorkerCompleted);

The call is made here from the main window class

  private void CSV_Load_Click(object sender, RoutedEventArgs e)
    ///Function to read csv into datagrid
    ///
    {
        //Turn Cursor to wait
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

        //Test connection to sql server
        if (CHHoursDataProvider.IsDatabaseOnline() == false)
        {
            System.Windows.Forms.MessageBox.Show("Can not establish contact with sql server" + "\n" + "Contact IT", "Connection Error");
            //Set UI picture
            return;
        }
        //Set a control to update the user here
        tbLoadDgStat.Visibility = Visibility.Visible;

        tbLoadDgStat.Text = "Getting data templete from Database...";
        string FilePath = txFilePath.Text;
        if (bwLoadCSV.IsBusy != true)
        {
            //load the context object with parameters for Background worker
            bwCSVLoadContext Context = new bwCSVLoadContext();
            Context.Site = cBChSite.Text;
            Context.FilePath = txFilePath.Text;
            Context.FileName = fileTest;
            Context.Wageyear = cbWageYear.Text;
            Context.Startdate = ((DateTime)dpStartDate.SelectedDate);
            Context.Enddate = ((DateTime)dpEndDate.SelectedDate);

            bwLoadCSV.RunWorkerAsync(Context);                

        }

The background worker do work is

private void bwLoadCSV_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        bwCSVLoadContext Context = e.Argument as bwCSVLoadContext;


        worker.ReportProgress((10));
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;

        }
        else
        {
            // Perform a time consuming operation and report progress load csv into datagrid.

To report the background work I do this. This is where I am trying to load a new form call ProgressDialog which has a progress bar on it, which I am try set to Indeterminable so it just "swishes" across my ProgressDialoge form to show the user something is still going on. I have used the reporter part of the background work because I believe it has access to the main window thread and I am hoping that the invoke method is then called from the main window thread, but I am not really sure

Here is the reporter

 private void bwLoadCSV_ProgressChanged(object sender, ProgressChangedEventArgs e)

    {


        this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Visibility = Visibility.Visible; });
        //tbLoadDgStat.Visibility = Visibility.Visible;


        //this.progressBar1.Value = e.ProgressPercentage;//This works but pauses on long steps
        if (e.ProgressPercentage == 10)
        {
            //Try to open a new form with a class ProgressDialog and set the progressbar
            // on the frm to IsIndeterminate=true
            //THIS IS NOT WORKING
            this.Dispatcher.BeginInvoke (new Action(() =>
              {  ProgressDialog progressDialog = new ProgressDialog();
                progressDialog.SetIndeterminate(true);
            }));

             //this updates the main form OK
             this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Getting data templete from Database..."; });

        }
        else if (e.ProgressPercentage == 20)
        {

            this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Data template retrieved..."; });

        }
        else
        {

            if (e.ProgressPercentage % 10 == 0)
            {
                this.Dispatcher.Invoke((MethodInvoker)delegate { tbLoadDgStat.Text = "Adding Data..." + e.ProgressPercentage.ToString() + "%"; });
            }
        }

Lastly the xaml for the ProgressDialog Form and it's class

<Window x:Class="Test_Read_CSV.ProgressDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Progress Dialog" Height="115" Width="306" Name="ProgressPopup">
<Grid>
    <ProgressBar Height="31" HorizontalAlignment="Left" Margin="12,33,0,0" Name="progressBar1" VerticalAlignment="Top" Width="250" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="7,4,0,0" Name="tbEvent" VerticalAlignment="Top" Width="254" IsReadOnly="True" IsEnabled="False" />
</Grid>

class

/// <summary>
/// Interaction logic for ProgressDialog.xaml
/// </summary>
public partial class ProgressDialog : Window
{
    public ProgressDialog()
    {
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        InitializeComponent();
    }

    public ProgressDialog(String Purpose)
    {
        InitializeComponent();
        tbEvent.Text = Purpose;
        WindowStartupLocation = WindowStartupLocation.CenterScreen;

    }
    public void UpdateProgress(int progress)
    {
        progressBar1.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                progressBar1.Value = progress;
            }
        ));
    }

    public void SetIndeterminate(bool isIndeterminate)
    {
        progressBar1.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                if (isIndeterminate)
                {
                    progressBar1.IsIndeterminate = true;
                }
                else
                {
                    progressBar1.IsIndeterminate = false;
                }
            }
        ));
    }
}

}

I have read and done a number of tutorial on background worker and even some on threads but can not seem to get the result I want

The idea is I have two long processes where I am either getting a datatable clone from my remote bd or I am updating the db from my wpf application (.net 4). While the process is running I want a progress bar control and to update it, for the obivous reason of making it clear that some work is going on. So I did the usual report progress routines in the background worker and it works....However, in my dowork thread I have this command

CHHoursDataProvider CH = new CHHoursDataProvider();
oTable = CH.CloneCHHours();

this where the communication with db is and this command takes a good 60 - 90 secs on a vpn remote connection so even if I do this

CHHoursDataProvider CH = new CHHoursDataProvider();
worker.ReportProgress((10));
oTable = CH.CloneCHHours();
worker.ReportProgress((20));

The main window still looks frozen and like it has crashed!

So all I want to do is at the start of the call to the background work is set a progressbar running and leave it running till the end of the task. This is all I need to do to finish my first ever project and after three days I still can not get my head around it!

So I have tried the follow

In the bw progress changed and in the main window class

 this.progressBar2.IsIndeterminate = true;

However the animation does not start till the Dowork thread has finished.

I then created another background worker to do update the progressbar2, which linked to a button on the main window was ok, but as soon as I tried to use it from the other background worker or from the main window class did not run till the dowork thread had completed on the first background worker

I then tried to follow a invoke method but REALLY got lost on that!

So can anyone help I can guess it is something to do with threading and working on the wrong thread etc but what I do about it I have no clue.

I can post more code as needed

Ian

Upvotes: 3

Views: 32702

Answers (1)

Sheridan
Sheridan

Reputation: 69985

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" />
</UserControl>

MainWindow XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Views="clr-namespace:WpfApplication1.Views"
    Title="MainWindow" Height="350" Width="525">
    <Views:TestView />
</Window>

UserControl code behind:

using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1.Views
{
    public partial class TestView : UserControl
    {
        private BackgroundWorker backgroundWorker = new BackgroundWorker();

        public TestView()
        {
            InitializeComponent();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.ProgressChanged += ProgressChanged;
            backgroundWorker.DoWork += DoWork;
            // not required for this question, but is a helpful event to handle
            backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                // Simulate long running work
                Thread.Sleep(100);
                backgroundWorker.ReportProgress(i);
            }
        }

        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // This is called on the UI thread when ReportProgress method is called
            progressBar.Value = e.ProgressPercentage;
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // This is called on the UI thread when the DoWork method completes
            // so it's a good place to hide busy indicators, or put clean up code
        }
    }
}

Upvotes: 19

Related Questions