Home All Groups Group Topic Archive Search About

extending BackgroundWorker ProgressChangedEventArgs

Author
15 Feb 2006 5:09 AM
Richard Bysouth
Hi

In my app I'm using a BackgroundWorker component to execute a long-running
task - updating a progress on my form fine. However, I'd like to provide more
information to that form while the process is running i.e. some info text
about the current task being executed.

So, what's the best way to do this? As far as I can see I will have to
inherit from the ProgressChangedEventArgs class and add additional
properties, however my inherited class won't be available to the
BackgroundWorker's ProgressChanged event.

thanks in advance

Richard

Author
15 Feb 2006 6:12 AM
Peter Huang" [MSFT]
Hi Richard,

In the ProgressChanged event, we can pass two parameters in the
ProgressChangedEventArgs.
1. ProgressPercentage(int)
2. UserState(object)

So we can pass the additional information in the paramater.
Here is some code snippet for your reference.

        private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
            this.label1.Text = e.UserState.ToString();
        }

        private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                Thread.Sleep(1000);
                string str = DateTime.Now.ToString() + ": Complete " +
(i*10).ToString() + "%";
                BackgroundWorker bw = (BackgroundWorker)sender;
                bw.ReportProgress(i * 10, str);
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
        {
            this.label1.Text = DateTime.Now.ToString() + " Completed.";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();
        }

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
15 Feb 2006 7:45 AM
Richard Bysouth
Peter

Thanks very much for the super-quick response...just what I was looking for.

I think that the documentation for this property could be improved though -
it doesn't really indicate that this is what it can be used for.

thanks

Richard
Author
15 Feb 2006 9:28 AM
Peter Huang" [MSFT]
Hi Richard,

Thanks for your feedback!
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.