public bool CancelAndWait()
{
lock (this)
{
// Set the cancelled flag
cancelledFlag = true;
// Now sit and wait either for the operation to
// complete or the cancellation to be acknowledged.
// (Wake up and check every second - shouldn't be
// necessary, but it guarantees we won't deadlock
// if for some reason the Pulse gets lost - means
// we don't have to worry so much about bizarre
// race conditions.)
while (!IsDone)
{
Monitor.Wait(this, 1000);
}
}
return !HasCompleted;
}