MULTIMEDIA

Silverlight : Response to Timer Events on the UI Thread

10/9/2010 4:42:15 PM
There are a few different timers you can use for triggering events at specific intervals, but recall that all UI updates must occur from the UI thread. You can always use BeginInvoke to marshal a delegate to the UI thread, but there’s a simpler way. WPF and Silverlight provide the DispatcherTimer class in the System.Windows.Threading namespace, which always fires on the UI thread. You can use this timer for updating the playback progress.
private System.Windows.Threading.DispatcherTimer timer =
new System.Windows.Threading.DispatcherTimer();

public MainPage()
{
InitializeComponent();
//one second
timer.Interval = new TimeSpan(0,0,1);
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
switch (videoPlayer.CurrentState)
{
case MediaElementState.Playing:
case MediaElementState.Buffering:
if (videoPlayer.NaturalDuration.HasTimeSpan)
{
double total = videoPlayer.NaturalDuration.TimeSpan.TotalMilliseconds;
if (total > 0.0)
{
double elapsed = videoPlayer.Position.TotalMilliseconds;
progressBar.UpdatePlaybackProgress(
100.0 * elapsed / total);
}
}
break;
default:
//do nothing
break;
}
}


Note

The DispatcherTimer is not Silverlight specific, and it can be used in any WPF app where you need a timer on the UI thread.


Figure 1 shows the video application with the progress bar in action.

Figure 1. Silverlight allows many of the same features of WPF, such as user controls.


Other  
 
Video
Top 10
Mobile Application Security : BlackBerry Security - Introduction to Platform
Windows 7 : Protecting Your Computer While Browsing (part 3)
IIS 7.0 : Securing Configuration - Securing Sensitive Configuration
AMD A6-3500 - Llano integrated-graphics processors
Macs no longer safe from virus attacks
Adobe Photoshop CS5 : Choosing the Right Process Version
Anatomy of Utrabooks (Part 2) - Acer Aspire S3
Silverlight Recipes : Updating the UI from a Background Thread
SQL Server 2008 : Using @@IDENTITY and NEWID Functions in DML Statements
Sharepoint 2010 : Business Connectivity Services Deployment Types (part 3) - Configure Indexing & Performing a Search
Most View
Go Abstract With Camera Dragging (Part 2)
Mobile Application Security : Mobile Geolocation - Geolocation Methods & Geolocation Implementation
Vengeance Game Keyboard Of Corsair - Mechanical Masterpieces Give You Control (Part 1)
Managing Local User Accounts and Groups in Vista
Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
Windows Azure : Processing with worker roles - Communicating with a worker role
A Complete Instruction For Your PC Kit And How To Buy (Part 3) - Samsung 300E5A-A01
Mobile Application Security : SMS Security - Protocol Attacks (part 2)
Manage iOS with iCloud (Part 2)
iPhone Application Development : Working with Text, Keyboards, and Buttons (part 4) - Hiding the Keyboard
Linux - Ninja Pi
Understanding Mobile Networking and Remote Access in Vista
Algorithms for Compiler Design: LOOP OPTIMIZATION
Mastering Windows PC (Part 2) - Use Taskbar, Windows Explorer & Control Panel
The Android Phone computer (Part 1) - Can I use a USB Keyboard or Mouse?
Windows 7 : Detecting and Resolving Computer Problems (part 1) - Solving the Tough Problems Automatically
Biggest tips guide ever! (Part 1)
Algorithms for Compiler Design: REPRESENTING THREE-ADDRESS STATEMENTS
Creating Windows with Mixed Content
Computing Yourself Fit (Part 3)