0

I am using a progress bar to show the progress to the user in a time consuming operation.My problem is,while i am doing the operation the backgroundworker can't report any progress until it is completed.So if i have only one iteration,the progressbar always shows 0% and when it is finished,the progressbar gets closed immediately.So the user does not gets the impression that the task is completed.I am looking for a way to let the user know that the task is completed before the progessbar gets closed.My code is as below.

            this.progressDialog = new progressDialog();

            Dispatcher pdDispatcher = ((Window)this.progressDialog).Dispatcher;

            this.worker = new BackgroundWorker();
            this.worker.WorkerSupportsCancellation = true;
            this.worker.ProgressChanged += new  ProgressChangedEventHandler(worker_ProgressChanged);
            this.worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                foreach (Element id in state.Elements)
                {
                    if (this.worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }

                    UpdateProgressDelegate update = new UpdateProgressDelegate(this.UpdateProgressText);
                    pdDispatcher.BeginInvoke(update, Convert.ToInt32(((decimal)state.Elements.IndexOf(id) / (decimal)state.Elements.Count()) * 100), state.Elements.Count());

                    Element newElement = this.Item.Copy(id);
                    if (newElement .HasValue)
                    {
                        state.CreatedElements.Add(newElement.Value);
                    }
                }
            };
            this.worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                if (!(args.Error == null))
                {
                    throw args.Error;
                } 
                this.UpdateDisplayItems();
                this.progressDialog.Close();
                this.progressDialog.Dispose();

            };

            this.worker.RunWorkerAsync();
            this.progressDialog.Show();            
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressDialog.Value = e.ProgressPercentage;
        }
    public void UpdateProgressText(int percentage, int recordCount)
    {
        this.progressDialog.Description = string.Format("{0}% of {1} Records", percentage.ToString(), recordCount);
        this.progressDialog.Value = percentage;
    }
4
  • I don't see you calling worker.ReportProgress anywhere in your DoWork function?
    – Dennis
    Feb 23, 2012 at 6:36
  • @Dennis I am using UpdateProgressText delegate to invoke the percentage to my progressdialog
    – biju
    Feb 23, 2012 at 6:46
  • progressDialog is custom dialog? Do you binding progressDialog.Value to ProgressBar Control? It would be nice if you can share progressDialog's code. Feb 23, 2012 at 7:13
  • why is it u're using the UpdateProgressText instead of the built in ReportProgress method?
    – dnr3
    Feb 23, 2012 at 7:39

2 Answers 2

2

In this type of situation, you might want to consider setting the IsIndeterminate property to true and forget about setting the value. This will give you a marquee effect, so the user get's the impression that something is happening.

1
  • @biju Yeah, if you're not getting any feedback from the worker thread then just marquee it and be done. Feb 23, 2012 at 6:51
0

Two options I can think of:

  1. When the work is done, don't close it immediately. Instead, set a timer to close it and set the progress to 100% for enough time for the user to see it.

  2. Round the percentage up by, say, 5 points, so the progress bar actually shows 100% complete for the time between 95 and 100%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.