David Chia
David Chia

Reputation: 1

C# mix timer and loop

I am creating a self enrollment system for a biometrics system. I was trying to create a loop mix with timer. I want to capture an image then stop the timer, and wait for 5 sec and capture again for 5 times. I was trying to use thread.sleep, but it will make my picturebox stop streaming the video. But according to the codes below, it will straight away capture 5 images , and non-stop looping. Please correct me. Thanks (5 sec timer > capture > timer stop ) * 5 times and finally all stop

private void timer1_Tick(object sender, EventArgs e)
{
   int a = 1;

   /* while loop execution */
   while (a < 5)
   {
        CaptureFunction();
        a++;
        timer1.Stop();
   }
   timer1.Start();
}

Upvotes: 0

Views: 822

Answers (2)

Pharap
Pharap

Reputation: 4082

You can change the timer's interval so it only fires every 5 seconds:

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.interval(v=vs.100).aspx

5000 is equivalent to 5 seconds. Then use this code:

int photosTaken = 0;

private void timer_Tick(object sender, EventArgs e)
{
     CaptureFunction();
     photosTaken++;
     if(photosTaken == 5) { timer.Stop(); }
}

If you wish to capture another 5, use this function:

private void capture5()
{
     photosTaken = 0;
     timer.Start();
}

Upvotes: 1

terrybozzio
terrybozzio

Reputation: 4542

This should solve:

    int a = 1;

    private void timer1_Tick(object sender, EventArgs e)
    {
         if(a <= 5)
         {
            CaptureFunction();
            a++;
         }
         else
            timer1.Stop();
         //place this just in case....
    }

oh...and if you really want to place the messagebox,place it after the increment(a++).

Upvotes: 0

Related Questions