Reputation: 1642
I'm creating a WinForm application that takes a person's photo with a webcam and am trying to now create a countdown effect. I have 4 images that i would like to cycle through but this is proving quite tricky to accomplish.
I'm using a timer for the seconds, but all that's happening is the app lags a bit and then the last image shows. Does anybody know how i might accomplish this?
Here's my code:
int counter = 0;
// start the counter to swap the images
tmCountDown.Start();
while (counter < 4)
{
// holding off picture taking
}
// reset counter for timer
counter = 0;
tmCountDown.Stop();
/// <summary>
/// timer event to switch between the countdown images
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tmCountDown_Tick(object sender, EventArgs e)
{
counter++;
//MessageBox.Show("c:/vrfid/apppics/" + counter + ".jpg");
pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg");
}
Upvotes: 4
Views: 26771
Reputation: 1
SET "INTERVAL =1000" in timer properties that's mean your timer every 1000 ms is refreshing And Then use an if(second == 10).....
Upvotes: 0
Reputation: 1642
Found a solution, no timer required. Thanks for the answers.
int counter = 0;
// start the counter to swap the images
while (counter < 4)
{
// holding off picture taking
counter++;
//MessageBox.Show("c:/vrfid/apppics/" + counter + ".jpg");
pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg");
pbCountDown.Refresh();
Thread.Sleep(1000);
}
// reset counter for timer
counter = 0;
Upvotes: 1
Reputation: 22906
The Windows Timer class uses the message queue for notifying the timer has expired. And so you need to have the message loop running in order to get the correct number of timer expires occuring. So you should set the counter variable to be a class field and then you can increment it inside the event handler. Something like this...
// Main Code
_counter = 0;
tmCountDown.Start();
// Event Handler
private void tmCountDown_Tick(object sender, EventArgs e)
{
_counter++;
if (_counter == 4)
tmCountDown.Stop();
else
pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + _counter + ".jpg");
}
Upvotes: 3
Reputation: 3419
The problem is that you are spinning in a busy loop while the timer is running. You should check the timer stop condition in the event handler.
I am also a bit surprised that the code works. If you are using System.Windows.Forms.Timer
, you should not even get into the event handler and so the counter should not be incremented. Also the counter value is not properly checked nor updated. The while loop can be transformed into endless loop.
Upvotes: 1
Reputation: 4253
You should use
counter++;
this.SuspendLayout();
pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg");
this.ResumeLayout();
I tested it and it was working, hope it helps you
Upvotes: 6