lpc

lpc

  • NA
  • 1
  • 0

Background Worker Cancel Issue

Aug 31 2009 10:38 AM
I have a method call within the background worker doWork event like this.

try
    {
          classname.method(argument, argument, argument);
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.Message);
          return;
    }


how i can implement a cancel to background worker. where to check for cancel pending. if i write the code like this

while (!bgworker.CancellationPending)
   {
         try
           {
               classname.method(argument, argument, argument);
            }
         catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
              return;
            }
     }   

     if (bgworker.CancellationPending
           e.Cancel = true;


then it keeps running again and again until the cancel button (bgworker.CancelAsync()) is not pressed. please help me i am new to C#. i search a lot but find examples which have loops in doWork, but i have method call.

Answers (2)