This topic has not yet been rated - Rate this topic

Clock Class

Maintains run-time timing state for a Timeline.

Namespace:  System.Windows.Media.Animation
Assembly:  PresentationCore (in PresentationCore.dll)
public class Clock : DispatcherObject

The Clock type exposes the following members.

  Name Description
Protected method Clock Initializes a new instance of the Clock class, using the specified Timeline as a template. The new Clock object has no children.
Top
  Name Description
Public property Controller Gets a ClockController that can be used to start, pause, resume, seek, skip, stop, or remove this Clock.
Public property CurrentGlobalSpeed Gets the rate at which the clock's time is currently progressing, compared to real-world time.
Protected property CurrentGlobalTime Gets the current global time, as established by the WPF timing system. 
Public property CurrentIteration Get the current iteration of this clock.
Public property CurrentProgress Gets the current progress of this Clock within its current iteration.
Public property CurrentState Gets a value indicating whether the clock is currently Active, Filling, or Stopped.
Public property CurrentTime Gets this clock's current time within its current iteration.
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property HasControllableRoot Gets a value that indicates whether this Clock is part of a controllable clock tree.
Public property IsPaused Gets a value that indicates whether this Clock, or any of its parents, is paused.
Public property NaturalDuration Gets the natural duration of this clock's Timeline.
Public property Parent Gets the clock that is the parent of this clock.
Public property Timeline Gets the Timeline from which this Clock was created.
Top
  Name Description
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method DiscontinuousTimeMovement When implemented in a derived class, will be invoked whenever a clock repeats, skips, or seeks.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GetCanSlip Returns whether the Clock has its own external time source, which may require synchronization with the timing system.
Protected method GetCurrentTimeCore Gets this clock's current time within its current iteration.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method SpeedChanged When implemented in a derived class, will be invoked whenever a clock begins, skips, pauses, resumes, or when the clock's SpeedRatio is modified.
Protected method Stopped When implemented in a derived class, will be invoked whenever a clock is stopped using the Stop method.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  Name Description
Public event Completed Occurs when this clock has completely finished playing.
Public event CurrentGlobalSpeedInvalidated Occurs when the clock's speed is updated.
Public event CurrentStateInvalidated Occurs when the clock's CurrentState property is updated.
Public event CurrentTimeInvalidated Occurs when this clock's CurrentTime becomes invalid.
Public event RemoveRequested Occurs when the Remove method is called on this Clock or one of its parent clocks.
Top

A Timeline, by itself, doesn't actually do anything other than describe a segment of time. It's the timeline's Clock object that does the real work: it maintains timing-related run-time state for the timeline.

In most cases, a clock is created automatically for your timeline. When you animate by using a Storyboard or the BeginAnimation method, clocks are automatically created for your timelines and animations and applied to their targeted properties. For examples, see How to: Animate a Property by Using a Storyboard and How to: Animate a Property Without Using a Storyboard.

You can also create a Clock explicitly by using the CreateClock method. In performance-intensive scenarios, such as animating large numbers of similar objects, managing your own Clock use can provide performance benefits.

Clocks are arranged in trees that match the structure of the Timeline objects tree from which they are created. The root clock of such a timing tree can be interactively manipulated (paused, resumed, stopped, and so on) by retrieving its Controller. Non-root clocks cannot be directly controlled.

Once created, a clock cannot be modified (but it can be manipulated).

Using a Timeline as a Timer

A timeline's clock will only progress when there's an event handler associated with it or (in the case of an AnimationClock object) it is associated with a property. For this reason (and others), it's not recommended that you use a Timeline as a timer.

Notes to Inheritors

Derived classes should implement GetCurrentTimeCore if they want to modify how time flows for this clock. Derived classes can be made to do additional work when the clock repeats, skips, seeks, begins, pauses, resumes, or stops by overriding the DiscontinuousTimeMovement, SpeedChanged, and Stopped methods.

This example shows how to use Clock objects to animate a property.

There are three ways to animate a dependency property:

Storyboard objects and the BeginAnimation method enable you to animate properties without directly creating and distributing clocks (for examples, see How to: Animate a Property by Using a Storyboard and How to: Animate a Property Without Using a Storyboard); clocks are created and distributed for you automatically.

The following example shows how to create an AnimationClock and apply it to two similar properties.


/*
    This example shows how to create and apply
    an AnimationClock.
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;


namespace Microsoft.Samples.Animation.TimingBehaviors
{
    public class AnimationClockExample : Page
    {

        ScaleTransform myScaleTransform;

        public AnimationClockExample()
        {

            this.WindowTitle = "Opacity Animation Example";
            this.Background = Brushes.White;
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Margin = new Thickness(20);

            // Create a button that with a ScaleTransform.
            // The ScaleTransform will animate when the
            // button is clicked.
            Button myButton = new Button();
            myButton.Margin = new Thickness(50);
            myButton.HorizontalAlignment = HorizontalAlignment.Left;
            myButton.Content = "Click Me";           
            myScaleTransform = new ScaleTransform(1,1);
            myButton.RenderTransform = myScaleTransform;


            // Associate an event handler with the
            // button's Click event.
            myButton.Click += new RoutedEventHandler(myButton_Clicked);

            myStackPanel.Children.Add(myButton);
            this.Content = myStackPanel;
        }

        // Create and apply and animation when the button is clicked.
        private void myButton_Clicked(object sender, RoutedEventArgs e)
        {

            // Create a DoubleAnimation to animate the
            // ScaleTransform.
            DoubleAnimation myAnimation = 
                new DoubleAnimation(
                    1, // "From" value
                    5, // "To" value 
                    new Duration(TimeSpan.FromSeconds(5))
                );
            myAnimation.AutoReverse = true;

            // Create a clock the for the animation.
            AnimationClock myClock = myAnimation.CreateClock();            

            // Associate the clock the ScaleX and
            // ScaleY properties of the button's
            // ScaleTransform.
            myScaleTransform.ApplyAnimationClock(
                ScaleTransform.ScaleXProperty, myClock);
            myScaleTransform.ApplyAnimationClock(
                ScaleTransform.ScaleYProperty, myClock);
        }
    }
}


For an example showing how to interactively control a Clock after it starts, see How to: Interactively Control a Clock.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ