Tuesday, December 29, 2009

Silver light 2.0 Timer

I have seen many forums asking about the timer so i thought i'd toss up a simple example.
To create timer have to use Dispatchtimer in System.Windows.Threading.
Go through the below example.
<Canvas>
<Canvas.Resources>
<Storyboard x:Name="Timer">
<DoubleAnimation Storyboard.TargetName="rectTimer" Storyboard.TargetProperty="Width" BeginTime="0:0:0" Duration="2"></DoubleAnimation>
</Storyboard>
</Canvas.Resources>
<Rectangle Loaded="rectTimer_Loaded" Visibility="Collapsed" x:Name="rectTimer">
</Rectangle>
</Canvas>

private void rectTimer_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0,1, 100);
myDispatcherTimer.Tick +=new EventHandler(myDispatcherTimer_Tick);
myDispatcherTimer.Start();
}
void myDispatcherTimer_Tick(object sender, EventArgs e)
{
HtmlPage.Window.Alert("TickEvent");
}