Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BackgroundWorker ProgressBar - HELP PLEASE!

Status
Not open for further replies.

30secondstosam

Programmer
May 25, 2011
1
0
0
GB
Hi all,

I'm new to C# so please bare with me on this one...
I'm having a bit of trouble making the background worker do it's stuff...
Code:

int chkd = 0;
int total = 0;

private void resize_all()
{
ImageProcessor ip = new ImageProcessor();

int chkd = (this.cbThbNail.Checked ? 1 : 0);
chkd+= (this.cbQXGA.Checked ? 1 : 0);
chkd+= (this.cbSVGA.Checked ? 1 : 0);
chkd+= (this.cbUXGA.Checked ? 1 : 0);
chkd+= (this.cbVGA.Checked ? 1 : 0);
chkd+= (this.cbXGA.Checked ? 1 : 0);
ip.total = this.listBox1.Items.Count * chkd;

// thumbnails
if (this.cbThbNail.Checked)
{
resize_eachsize(ip, 75, 75);
}
}

private void resize_eachsize(ImageProcessor ip, int imgHeight, int imgWidth)
{
foreach (string img in this.listBox1.Items)
{
ip.imageResize(imgHeight, imgWidth, img, this.dirFinBox.Text);
backgroundWorker1.ReportProgress((int)(ip.done / ip.total) * 100);
}
}


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(0);
resize_all();
backgroundWorker1.ReportProgress(100);
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar.Value=e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// re-enables form - shows process as complete
MessageBox.Show("Process Complete!");
this.progressBar.Value = 0;
this.Enabled = true;
}



---------

I know I'm probably doing something blindingly obvious but could someone please give me a nudge in the right direction?

Many thanks for your help!
 
It looks like you have 2 separate code paths.

I was originally going to point you at this:
Code:
backgroundWorker1.ReportProgress(0);
resize_all();
backgroundWorker1.ReportProgress(100);
You only set the progress to 100 and never anything inbetween.

but then you have the other one:
Code:
ip.imageResize(imgHeight, imgWidth, img, this.dirFinBox.Text);
backgroundWorker1.ReportProgress((int)(ip.done / ip.total) * 100);

This doesn't appear to actually be threaded code, so I'd normally suggest putting this right after you recalculate the progress.
Code:
System.Windows.Forms.Application.DoEvents();

Lodlaiden

I'll answer your question, not solve your problem
 
I'm not sure if this will help... but here is an example done in Silverlight (it just happens to be what I have open - I don't believe it uses anything Silverlight exclusive aside from the UI controls). As you can see, it just loops 1-100 and reports progress along the way. It also supports canceling.

Hopefully it will help you make heads or tails of the process you're going for.

Good luck!

-Mark

Declare the background worker
Code:
BackgroundWorker bw = new BackgroundWorker();

On page load/control load, etc
Code:
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;

The long running process you want on a different thread
Code:
private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; (i <= 100); i++)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    //Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(50);
                    worker.ReportProgress((i ));
                }
            }
        }

What to do when progess changes
Code:
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.txtbxProgress.Text = (e.ProgressPercentage.ToString() + "%");
            this.progbar.Value = e.ProgressPercentage;
        }

What to do when completed
Code:
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.txtbxProgress.Text = "Canceled!";
            }

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

            else
            {
                this.txtbxProgress.Text = "Done!";
            }
            RemoveEventHandlers();
        }

Attach handlers and fire off the process (button click in this case)
Code:
private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!bw.IsBusy)
            {
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                this.progbar.Value = 0;
                bw.RunWorkerAsync();
            }
        }

Method to remove handlers (called in the completed event)
Code:
private void RemoveEventHandlers()
        {
            bw.DoWork -= new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged -= new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }

cancel button wire up
Code:
private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (bw.WorkerSupportsCancellation == true)
            {
                bw.CancelAsync();
            }

        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top