Your code is wrong, but not for the reason Ignacio says.
I'm replying to this post, because in spite of the more detailed post you
offered, it seems your pseudocode is pretty close to what you're actually
doing. And it's broken. Here's why...
I'm calling a background worker that calls a form...
example (this is a little like pseudocode):
longTimeMethod()
{
progressWorker.RunWorkerAsync();
while (counter < total)
{
progressValue = total/counter
counter++
}
bDone = true;
}
So, you run a progress dialog on a BackgroundWorker, while doing all of
the actual work on the original thread. This is exactly backwards from
the intended use of BackgroundWorker.
progressWorker_DoWork()
{
ProgressForm progress = new progressForm();
progress.ShowDialog();
while(!bDone)
{progress.percent = progressValue;}
progress.Close();
bDone = false;
}
The above is the main problem. ShowDialog() will block until the form is
closed. So the loop where you pool "bDone" doesn't get executed until the
dialog form is closed.
However, since you're misusing BackgroundWorker anyway, it would be more
fruitful to show you how to correctly use it. Again in pseudocode:
ProgressForm pf = new ProgressForm();
void initiator()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += worker;
bw.ProgressChanged += progress;
bw.RunWorkerCompleted += done;
bw.RunWorkerAsync();
pf.ShowDialog();
}
void worker()
{
// Here you do all the stuff you have in your "DeleteEvents" method
// Call bw.ReportProgress when you want the progress dialog to be
updated
// e.g. once per loop iteration
}
progress()
{
// update your ProgressForm here. E.g. ProgressForm.Progress =
e.ProgressPercentage
// or similar as appropriate
}
done()
{
// close your ProgressForm here. When you do that, this will
cause the
// call to ShowDialog() found in the initiator() method to return,
and
// your code execution will proceed from there.
}
Note that with BackgroundWorker there is no need to use Invoke(), as it
runs the ProgressChanged and RunWorkerCompleted events on the original
thread. Because of this, and because they can't be run until you get back
into a message pump, even if the worker finishes before you get to show
your progress dialog, I don't think there should be any trouble closing
the dialog from the done() method, since you shouldn't be able to get in
there until the form has been shown and a message pump loop has been
entered again (implicit in the call to ShowDialog()).
Pete