Example of UserState for BackgroundWorker

J

Jakanapes

I'm trying to write my first program using the backgroundworker and am
looking for some example code.

I kow that you can use the UserState member to pass more information
back to the UI for updating on a progressChanged call, but all the
examples I've seen show the ProgressPercentage.

Could anyone point me to a UserState example on the web or show me some
quick example code?

TIA

Jak
 
G

Guest

You can use any objects as a UserState. In the following example, the
UserState is a String Array:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(100, new String[] { "Hello",
"World" });
}

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
String[] str = (String[])e.UserState;

MessageBox.Show("Percentage: " + e.ProgressPercentage + ", " +
str[0] + " " + str[1]);
}
 
J

Jakanapes

That helps a bit....thanks.

I'm trying to send back a class to ProgressChanged and not quite sure
how to accomplish this.

I'm used to C++ on Unix and this VC++ is throwing me a bit.

Do I need to do something like this?


System::Object^ out = gcnew userClass;
//stuff some values in out
worker->ReportProgress( 0, out);

and then try to get my values from the Object and put them in a new
userClass for use?

I'm trying to RTFM, but any help is appreciated. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top