Hi all,
I am new to threading and was wondering if this works. I have a main thread that kicks off a background worker thread. It's function is to update my sql db. There are ten steps and after each step is completed, it will update the progressbar and move on to the next step. Three of the steps, say 5, 6, and 7, are independent from one another and can potentially be run at the same time. I then go about creating three new threads in backgroundworker and wait for them to all finish before joining them to the backgroundworker. It will then move on with the rest of the steps.
The problem I'm seeing is that while the three threads are doing their thing, the backgroundworker doesn't wait for them to finish and proceeds to go on with the rest of the steps. Wouldn't the Thread.join() force the backgroundworker to wait until those threads are complete before moving on? Am I making it more complicated then it really is?
Thanks for the help!
Here is a simplified version of my code:
private void bwAsync_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bwAsync = sender as BackgroundWorker;
int progressval = 0;
string sql = string.Empty;
while (progressBar1.Value != 100)
{
if (bwAsync.CancellationPending)
{
Thread.Sleep(1000);
e.Cancel = true;
return;
}
else
{
if (progressBar1.Value < 10)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 20)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 30)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 40)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 70)
{
thrd1= new Thread(new ThreadStart(sfsp1.Run)); --sfsp1.Run updates/edits db.
thrd1.Start();
thrd2= new Thread(new ThreadStart(sfsp1.Run)); --sfsp1.Run updates/edits db.
thrd2.Start();
thrd3= new Thread(new ThreadStart(sfsp1.Run)); --sfsp1.Run updates/edits db.
thrd3.Start();
thrd1.join();
thrd2.join();
thrd3.join();
update progressval
}
else if (progressBar1.Value < 80)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 90)
{
update/edit db.
update progressval
}
else if (progressBar1.Value < 100)
{
update/edit db.
update progressval
}
bwAsync.ReportProgress(progressval);
Thread.Sleep(100);
}
}
}