Reputation: 2546
I have been scratching my head all day on this one and it is infuriating me, is there any obvious problems with this that I do not know about ?
Here is the code:
private Timer _timer = null;
private EventHandler ev = null;
private void startAnimatingPicStatus()
{
Console.WriteLine(" | STARTING ANIMATION");
_timer = new Timer();
_timer.Interval = 100;
ev = new EventHandler(timer_Tick);
_timer.Tick += ev;
_timer.Start();
}
private int animationPosition = 0;
private void timer_Tick(object sender, EventArgs e)
{
Console.WriteLine(" | ANIMATING ANIMATION");
animationPosition++;
if (animationPosition == 4) animationPosition = 1;
Dictionary<int, Image> images = new Dictionary<int, Image>();
images[1] = myproject.Properties.Resources.animateimage_1;
images[2] = myproject.Properties.Resources.animateimage_2;
images[3] = myproject.Properties.Resources.animateimage_3;
picStatus.Image = images[animationPosition];
}
private void stopAnimatingPicStatusIfAnimatingAndSetToComplete()
{
Console.WriteLine(" | STOPPING ANIMATION");
_timer.Tick -= ev;
_timer.Dispose();
picStatus.Image = nofolder.Properties.Resources.tfolderg;
}
this is the debug I would expect:
| STARTING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| STOPPING ANIMATION
but this is what I get:
| STARTING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| STOPPING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
| ANIMATING ANIMATION
the good news is that it does seem to stop the animation. the bad news is it is only sometimes and it is random but seems to happen 50% of the time - 50% of the time the animation stops and about 50% of the time it just spins animating even though it should have stopped
EDIT>SOLUTION I added if (ev != null) _timer.Tick -= ev;
inside startAnimatingPicStatus()
right before the line that says ev = new EventHandler(timer_Tick);
and this seems to have fixed it
Upvotes: 0
Views: 110
Reputation: 6065
To stop your timer, have you tried to use the Change method, just like :
myTimer.Change(Timeout.Infinite, Timeout.Infinite);
More information here : https://msdn.microsoft.com/en-us/library/yz1c7148%28v=vs.110%29.aspx
Upvotes: 1