Search code examples
c#timervideo-recording

Timer that starts another timer - wrong interval


I have made a small surveillance tool that records images from a webcam. To keep the amount of data sleek, instead of recording a video I simply save three snapshots each three seconds apart and repeat this every full minute.

I have added two timers to my form, timerMin with an intervall of 60,000, and timer3Sec with an interval of 3000, added event handlers and start the minute-timer upon form load:

timerMin.Tick += timerMin_Tick;
timer3Sec.Tick += timer3Sec_Tick;
timerMin.Start();

Then I start the 3 second timer from the minute's tick event:

private void timerMin_Tick(object sender, EventArgs e)
{
    timer3Sec.Start();
}

In the 3 second timer's tick event, I save the snapshots and increment a counter, so I know when 3 snaps have been saved:

private void timer3Sec_Tick(object sender, EventArgs e)
{
    SaveSnap((Bitmap)pictureBox1.BackgroundImage);
    Ticks++;
    if (Ticks > 2)
    {
        timer3Sec.Stop();
        Ticks = 0;
    }
}

"Ticks" is a public int, intialised with 0. I expected this to result in three snaps per minute. Alas, I only get one per minute. Where am I going wrong with my thinking?


Solution

  • Make sure to generate different filenames in your Savesnap method.

    Be wary of DateTime.ToShortTimeString, which goes down to minutes only. ;-)