I have an app that I am trying to build using the BackgroundWorker Component, and I am having a little bit of trouble figuring out how to pass multiple paramaters back with ReportProgress. Below you can see the two small code snippets.
Any help would be greatly appreciated.
Thanks,
Russ
//////////////////////////////////////////////////////////////////////////////////////////////////////
(sender
as BackgroundWorker).ReportProgress(currentCount, totalCount);//////////////////////////////////////////////////////////////////////////////////////////////////////
void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e){
progressBar1.Value = (e.ProgressPercentage);
progressBar1.Maximum = ( );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////

Passing two paramaters
Vaish
The second parameter you're passing with the ReportProgress method is a "user state" parameter, which can hold any type of object. If you pass an integer value through this parameter, you can retrieve its value like:
progressBar1.Maximum = (int)e.UserState;
However, if you need to pass even more parameters, you can create a class with a few properties you need and pass that. Then, in ProgressChanged handler, cast e.UserState to that class and read its properties.
Andrej
chaz1
You would only start the second thread from the first thread's RunWorkerCompleted event, which will trigger when the first thread is finished processing.
Andrej
Oscarfh
It works perfectly, thanks a lot.
Russ
Kannan.B
Ok, I guess I should have figured that out.
There is one more thing that I am trying to figure out, and I think that after that I am set with this program. Let's say that I have two classes:
ghv_sql_res
ghv_sql_res_2
Now let's say that ghv_sql_res prepares all of my results, and then ghv_sql_res_2 then takes those results and writes them to my database. I need ghv_sql_res_2 to run only when ghv_sql_res has completed, and not at the same time. I tried to do the following, but that runs them simultaneously:
ghv_sql_res
sqlProcess = new ghv_sql_res();backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(sqlProcess.ghv_sql_res_process);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.RunWorkerAsync();
ghv_sql_res_2 sqlProcess_2 = new ghv_sql_res_2();
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(sqlProcess_2.ghv_sql_res_2_process);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.RunWorkerAsync();
If I wanted to tell my program to wait until the first class was done executing, would I use a sleep command, or is there part of the backgroundWorker component that would give these instructions more efficiently
Thanks for all of your help, you've simplified everything greatly.
Thanks,
Russ
cb3431
BackgroundWorker also has RunWorkerCompleted event, which is triggered when your class is done processing. Run the first class. In its DoWork handler, return the results as a Result parameter. You can access this parameter in the RunWorkerCompleted event. Read the result here and trigger your second class.
private void worker_DoWork(object sender, DoWorkEventArgs e){
// Do your work here
e.Result = "return results which your next class will use";
} private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string result = (string)e.Result;
// Pass this result as an entry parameter for processing your next class
// In this case the result is a string, but it can be any other type
}
Andrej
Fabrice MARGUERIE
Ok, that works perfect, thank you so much.
Another quick question, how do I pass a parameter to the child thread using the BackgroundWorker component
Thanks,
Russ
Will Merydith
Ok, that makes sense, but how is the best way to stop my second thread from triggering until that point. Am I to have it sleep with a loop
Thanks,
Russ
Nikhil Vaghela
You can pass a parameter with the RunWorkerAsync() method:
worker.RunWorkerAsync(500);
and read it in its DoWork event handler:
void worker_DoWork(object sender, DoWorkEventArgs e){
int parameter = (int)e.Argument;
} Andrej