-1

DoWork is this:

        DoLengthyWork();

        //this is never executed
        if(bgWorker.CancellationPending)
        {
            MessageBox.Show("Up to here? ...");
            e.Cancel = true;
        }

I trigger the cancelation in with bgWorker.CancelAsync();

If bgWorker.CancellationPending cannot be reached because it's blocked by DoLengthyWork HOW can I stop the BackGround Worker ?

kawa
  • 422
  • 4
  • 16
  • Check this: http://stackoverflow.com/questions/4732737/how-to-stop-backgroundworker-correctly – Yuval Itzchakov Jun 07 '14 at 11:07
  • 1
    you need to get that `bgWorker.CancelAsync()` trigger inside your `DoLengthyWork()` so you can create a safe method to cancel the pending job. .NET does not cancel the job for you, it only set's to be canceled through `CancellationPending` property. – balexandre Jun 07 '14 at 11:37
  • @Yuval, All that thread does is explain why my problem can't be solved. Thanks anyway ! ;) – kawa Jun 07 '14 at 11:51
  • It explains what has to be done.. :) – Yuval Itzchakov Jun 07 '14 at 12:13

1 Answers1

0

Your problem is that the execution of your DoWork function won't reach CancellationPending condition ever. In fact, As balexandre said, you should know that organize DoLengthyWork scenario to check CancellationPending condition periodically inside it.

Mojtaba
  • 2,764
  • 1
  • 21
  • 24
  • If you read my question completelly you would have noticed that I know what my problem is! – kawa Jun 07 '14 at 12:10