1

Possible Duplicate:
How To Start And Stop A Continuously Running Background Worker Using A Button

I have 2 buttons the first one it's name "Continuous" .. the second one "Stop"

I want to call a method when press the continuous button :

private void continuous_Click(object sender ,EvantArgs e) 

      {   

     // continuous taking pictures ...

      }

my question is : how can I stop the execution by pressing the stop button ??

I've written a code to take a picture and I've succeeded to take pictures ... now I want the camera to take continuous snapshots ... but if I press stop button the camera should stop taking pictures ...

I've used BackGroundWorker but the code does not work !!!

this is the code :

private void ContinousSnaps_Click(object sender, EventArgs e)
    {

        Contiguous.DoWork += Contiguous_DoWork;
        Contiguous.RunWorkerCompleted += Contiguous_RunWorkerCompleted;
        Contiguous.RunWorkerAsync();
    }

    private void Contiguous_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; ; i++) TakeSnapShotCommand();
    }

    private void Contiguous_RunWorkerCompleted(object sender,
                                   RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("complete");
    }

    //------------------------------------------------------------------//

    private void Stop_Click(object sender, EventArgs e)
    {
        Contiguous.CancelAsync();


    }

  //--------------------------------------------------------------------//

how can I achieve the result that I want ?!

Community
  • 1
  • 1
lolo
  • 39
  • 9
  • Contiguous is the name of BackGroundWorker control that I've added to the form – lolo Jan 26 '13 at 04:06
  • "but the code does not work !!!" is not a useful description of the actual issue. You'll get better answers by being more specific/descriptive. – RJ Lohan Jan 26 '13 at 04:06

1 Answers1

4

Try and see if this is going to work: In your _DoWork event:

    private void Contiguous_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; ; i++)
        {
            if (Contiguous.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            TakeSnapShotCommand();
        }
    }

And in the Stop_Click to the following:

    private void Stop_Click(object sender, EventArgs e)
    {
        if (Contiguous.WorkerSupportsCancellation)
            Contiguous.CancelAsync();
    }

Also make sure you allow cancellation (and if you want to take my advice here - move these event registrations in a the form load, so they will be executed once, not every time the button is clicked - leave just the Continuous.RunWorkerAsync()):

    // your form load <---
    private void Form1_Load(object sender, EventArgs e)
    {
        Contiguous.DoWork += Contiguous_DoWork;
        Contiguous.RunWorkerCompleted += Contiguous_RunWorkerCompleted;
        Contiguous.WorkerSupportsCancellation = true; // allowing cancellation
    }

    private void ContinousSnaps_Click(object sender, EventArgs e)
    {
        // not a bad idea if you disable the button here at this point
        Contiguous.RunWorkerAsync();
    }
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • 1
    The Click event handler is quite buggy, it causes too many event handlers to get registered. – Hans Passant Jan 26 '13 at 04:16
  • I agree, this code has to be moved somewhere else and it has to be executed once. I've mentioned in my edit. – Dimitar Dimitrov Jan 26 '13 at 04:17
  • @ Dimitar Dimitrov .. Thank u very much :), I've faced this error :( ---> 'object' does not contain a definition for 'CancellationPending' and no extension method 'CancellationPending' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) – lolo Jan 26 '13 at 13:55
  • Yep, sorry, fixed, give this a shot. Contiguous.CancellationPending ... – Dimitar Dimitrov Jan 26 '13 at 14:25
  • Thank u so much ...there is no problems now with BackGroundWorker thanks alot ,, :)))))) – lolo Jan 26 '13 at 16:26