1

I am trying to fill a DataTable from database in background worker, and i want to show a progress update form meanwhile. I know i can use a loop to show the progress, but how can i show the progress as it progress to fill the table?

This is the code that i have used to show a progress bar in a new form:

private void backgroundWorker1_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.
                    worker.ReportProgress(i * 10);
                    System.Threading.Thread.Sleep(500);
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Pass the progress to AlertForm label and progressbar
            alert.Message = "In progress, please wait... " + e.ProgressPercentage.ToString() + "%";
            alert.ProgressValue = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {

            }
            else if (e.Error != null)
            {

            }
            else
            {

            }
            // Close the AlertForm
            alert.Close();
            button1.Enabled = true;
        }
Usman Farooq
  • 109
  • 3
  • 11
  • [Show Loading animation during loading data in other thread](https://stackoverflow.com/a/39142535/3110834) – Reza Aghaei Jan 20 '18 at 12:02
  • @RezaAghaei is it possible to update progress bar with this ? – Usman Farooq Jan 20 '18 at 12:04
  • The linked post is not using a `ProgressBar`. It's just using a loading animated gif. To use a `ProgressBar` you need to load data in multiple requests (getting for example 1000 records at each request). The solution depends to the number of records. Usually showing a loading image is enough, because if the number of records is such high which you need to load it in multiple requests, it means the data is not suitable to show in a grid at a single shot. – Reza Aghaei Jan 20 '18 at 12:08
  • If by "fill a DataTable" you mean with an Adapter and a SelectCommand then you have no way to know its progress, other than not filled and then filled. – Crowcoder Jan 20 '18 at 12:41
  • Yes, exactly that @Crowcoder – Usman Farooq Jan 20 '18 at 12:43

0 Answers0