Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopwatch for silverlight?

Tags:

silverlight

There is no StopWatch for Silverlight.

What would you use instead of it?

I saw some posts about people saying to create an empty animation and call GetCurrentTime() from the Storyboard class, I couldn't get it to work...

What would you do?

like image 861
Paulo Avatar asked May 23 '09 06:05

Paulo


3 Answers

This is what I did. Its simple and worked very well for me:

long before = DateTime.Now.Ticks;
DoTheTaskThatNeedsMeasurement();
long after = DateTime.Now.Ticks;

TimeSpan elapsedTime = new TimeSpan(after - before);
MessageBox.Show(string.Format("Task took {0} milliseconds",
    elapsedTime.TotalMilliseconds));

All you needed to do was keep one long variable holding the total ticks before beginning the target task.

like image 187
moonlightdock Avatar answered Nov 17 '22 11:11

moonlightdock


See my watch at here. Source code is here. And two articles about it are here and here. The Silverlight animation model lends itself well to a stopwatch.

alt text http://xmldocs.net/ball2/background.png

<Storyboard x:Name="Run" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    Storyboard.TargetName="SecondHand" x:Name="SecondAnimation" 
    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:01:00" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    Storyboard.TargetName="MinuteHand" 
    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="01:00:00" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
    Storyboard.TargetName="HourHand" 
    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="12:00:00" Value="360"/>

and the code to run that storyboard is here:

private void RunWatch()
{
    var time = DateTime.Now;

    Run.Begin();

    Run.Seek(new TimeSpan(time.Hour, time.Minute, time.Second));
}
like image 27
Michael S. Scherotter Avatar answered Nov 17 '22 11:11

Michael S. Scherotter


Here is a Stopwatch replacement class that you can add to your project.

like image 4
Bryan Legend Avatar answered Nov 17 '22 11:11

Bryan Legend