/// <summary> /// Stops the current animation, if any. /// </summary> public void StopAnimation() { // An animation is uniquely associated with a timer. To stop the // animation, we stop and dispose the timer associated with it. if (timer != null) { timer.Stop(); timer.Dispose(); timer = null; // Raise the AnimationEnded event. if (AnimationEnded != null) AnimationEnded(this, null); // TODO: maybe it's logically clearer if we move the image and // tag into EventArgs. image = null; tag = null; } }
/// <summary> /// Starts animating an image, optionally associated with a piece of /// user-defined data. If an animation is already in progress, it is /// stopped first. /// <param name="image">The image to be animated.</param> /// <param name="tag">User-defined data to associate with this /// animation.</param> /// </summary> public void StartAnimation(Util.Media.ImageDecoder image, object tag) { if (image == null) throw new ArgumentNullException("image"); // Stop any existing animation. StopAnimation(); // Start a new animation. We create a new Timer object for each // animation, so that if we get a WM_TIMER message after the timer // is stopped, we can discard the message. This is guaranteed as // long as the WM_TIMER message handler is called in the same // thread where StopAnimation() is called. this.image = image; this.tag = tag; this.timer = new System.Windows.Forms.Timer(); this.timer.Tick += new EventHandler(this.timer_Tick); this.currentIndex = -1; PlayNextFrame(); }