By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,582 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,582 IT Pros & Developers. It's quick & easy.

extending BackgroundWorker ProgressChangedEventArgs

P: n/a
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
Feb 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Feb 15 '06 #2

P: n/a
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
Feb 15 '06 #3

P: n/a
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.

Feb 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.