On Thu, 11 Sep 2008 13:17:01 -0700, jp2msft
<jp*****@discussions.microsoft.comwrote:
I copied their example, and I even have my thread checking to see if it
has
been cancelled within its loops:
if (worker.CancellationPending == true) {
e.Cancel = true;
return;
}
This part of the code is triggered, but when this happens, the code does
not
enter the bgWorker_ProgressChanged or bgWorker_RunWorkerCompleted event
handlers.
Because of this, my do...while loop never exits. [...]
Unless you post a concise-but-complete code sample that demonsrates
exactly what you're doing, it's not possible to say what you're doing
wrong. The RunWorkerCompleted event definitely should be raised, but
there's nothing in the code you post that proves that you're handling it,
never mind that it would affect the loop you posted.
The most likely explanation is that the loop you posted is executing on
the main GUI thread, and of course doing so would cause all sorts of
issues, including this one. But without all the code, it's impossible to
say for sure what's going on.
That said, I'll point out that just from the little code you've posted,
it's clear that your basic strategy is simply wrong. There is no sense in
handing off work to a different thread only to have the current thread sit
and wait for it to be done. There also is _especially_ no sense in
writing a "busy wait" as you've done here, where the only thing the loop
does is repeatedly check for a specific condition. Even in situations
where it makes sense for a thread to wait for something to happen (and
this doesn't appear to be such a situation), there are correct ways to
wait, such as using a WaitHandle sub-class, that don't consume CPU
resources uselessly as your code does.
If you can provide a concise-but-complete code sample, I'm sure I or
someone else can provide more useful advice. Until then, I remain
unoptimistic. :)
Pete