1

Guys I am trying to learn wpf and so I have made an application which has a task to update some records in SQL db tables. Now as there are thousands of records, it takes some time to update all the records, so what I wanna do is, show a progress (by using progress bar or showing % in label) during the process of record updation.

Now I have been reading about backgroundworker and how to show progress but I am not still clear about the whole concept. Now by following some tutorials, I have tried to implement backgroundworker and progress but the problem is, I am stuck in a loop.

My MainWindow.xaml.cs Code:

public MainWindow()
{
    InitializeComponent();
    bw.WorkerReportsProgress = true;
    bw.WorkerSupportsCancellation = true;
}

private BackgroundWorker bw = new BackgroundWorker();

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

    for (int i = 1; (i <= 10); i++)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress((i * 10));
            StudyMode.Update up = new Update();
            up.UpdateCompanyList();
        }
    }
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.label1.Content = (e.ProgressPercentage.ToString() + "%");
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if ((e.Cancelled == true))
    {
        this.label1.Content = "Canceled!";
    }

    else if (!(e.Error == null))
    {
        this.label1.Content = ("Error: " + e.Error.Message);
    }

    else
    {
        this.label1.Content = "Done!";
    }
}


private void btnStart_Click(object sender, RoutedEventArgs e)
{
    if (bw.IsBusy != true)
    {
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();
    }
}

private void btnEnd_Click(object sender, RoutedEventArgs e)
{
    if (bw.WorkerSupportsCancellation == true)
    {
        bw.CancelAsync();
    }
}

In StudyMode class, I have :

public void UpdateCompanyList()
 {
            try
            {
                for (int i = 1; i <= EndLimit; i++) //loop end limit dynamic
                {
                    string Url = "..some API link....";
                    Parser objParser = new Parser();
                    objParser.UpdateCompanyList(Url);
                }
                MessageBox.Show("Company Names Updated");
                SomeMethod(); //this method takes time as it updates all the company profiles..
                MessageBox.Show("Company Profiles Updated");
            }
            catch (Exception ex)
            {
                // Logging exception here
            }
        }

Now as you can see, in my method, I have two MessageBox. Now when I run the above code and press the start button, UpdateCompanyList executes as I get two messagebox then the label gets updated to 10% and then the messageboxes appear again and then label gets updated to 20% and so on. So the method repeats 10 times (100%) and then stops executing. What am I doing wrong ? How can I make it to update progress while its executing UpdateCompanyList() method?

NewbieProgrammer
  • 874
  • 2
  • 18
  • 50
  • Please show us the `UpdateCompanyList();` method. But if you want to report progress in that method then just use something like `worker.ReportProgress(variable);` – jay_t55 Sep 24 '14 at 09:39
  • Why didn't you follow the advice in the answers to the questions that were suggested to you *before* you wrote your question? There were lots of them. – Sheridan Sep 24 '14 at 09:40
  • What you doing wrong? Who knows...is your message boxes should pop up by your design or not? – Renatas M. Sep 24 '14 at 09:44
  • MessageBox should show up but only once. The `UpdateCompanyList` method should only be executed one time. Not 10 times. – NewbieProgrammer Sep 24 '14 at 09:46

0 Answers0